如果实例对象值和数组值相同,则删除数组索引

时间:2018-07-18 17:45:13

标签: arrays swift indexing closures

我有一个名为 var geoLocList = [GeolocationModel]()

的数组

和一个名为 var geoLocs = GeolocationModel()

的对象

Severity Code Description Project File Line Suppression State Error The "LinkAssemblies" task failed unexpectedly. Java.Interop.Tools.Diagnostics.XamarinAndroidException: error XA2006: Could not resolve reference to 'Android.Gms.Common.Internal.SafeParcel.Zza' (defined in assembly 'Xamarin.GooglePlayServices.Places, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null') with scope 'Xamarin.GooglePlayServices.Basement, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. When the scope is different from the defining assembly, it usually means that the type is forwarded. ---> Mono.Cecil.ResolutionException: Failed to resolve Android.Gms.Common.Internal.SafeParcel.Zza at Mono.Linker.Steps.MarkStep.HandleUnresolvedType(TypeReference reference) at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference) at MonoDroid.Tuner.MonoDroidMarkStep.MarkType(TypeReference reference) at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference) at MonoDroid.Tuner.MonoDroidMarkStep.MarkType(TypeReference reference) at Mono.Linker.Steps.MarkStep.MarkField(FieldReference reference) at Mono.Linker.Steps.MarkStep.MarkFields(TypeDefinition type, Boolean includeStatic, Boolean markBackingFieldsOnlyIfPropertyMarked) at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference) at MonoDroid.Tuner.MonoDroidMarkStep.MarkType(TypeReference reference) at Mono.Linker.Steps.MarkStep.InitializeType(TypeDefinition type) at Mono.Linker.Steps.MarkStep.InitializeType(TypeDefinition type) at Mono.Linker.Steps.MarkStep.InitializeAssembly(AssemblyDefinition assembly) at Mono.Linker.Steps.MarkStep.Initialize() at Mono.Linker.Steps.MarkStep.Process(LinkContext context) at Mono.Linker.Pipeline.Process(LinkContext context) at MonoDroid.Tuner.Linker.Process(LinkerOptions options, ILogger logger, LinkContext& context) at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res) --- End of inner exception stack trace --- at Java.Interop.Tools.Diagnostics.Diagnostic.Error(Int32 code, Exception innerException, String message, Object[] args) at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res) at Xamarin.Android.Tasks.LinkAssemblies.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() ElGas.Android 后面附加的geoLocs的值。

问题是,我试图根据相同的ID将geoLocList中的索引替换为geoLocList的最新值。

我仍然是iOS应用开发的新手。 老实说,我不太了解下面的代码。我从浏览过的一些参考资料中找到了某个地方,并认为这样做可以成功。但是我错了。

这是代码

geoLocs

1 个答案:

答案 0 :(得分:0)

这是一个使用问题中的if子句的示例,该子句可能会帮助您找到代码中的错误。我是使用Xcode游乐场编写的。

import Cocoa

struct Geo: CustomStringConvertible { //Simple struct used instead of GeolocationModel
    var id: Int //Identifier 
    var text: String

    var description: String {.  // added for better print() output
        return "\(id) - \(text)"
    }
}

var geoList = [Geo]()
var geo1 = Geo(id: 1, text: "First")
print("\(geo1) created")

if let index = geoList.index(where: { $0.id == geo1.id }){
    geoList.remove(at: index)
   print("geo removed")
}
geoList.append(geo1)
print("List contains: \(geoList)")

var geo2 = Geo(id: 1, text: "Second")
print("\(geo2) created")
if let index = geoList.index(where: { $0.id == geo2.id }){
    geoList.remove(at: index)
    print("geo removed")
}
geoList.append(geo2)
print("List contains: \(geoList)")

运行此命令时的输出是

  

1-首次创建
  列表包含:[1-第一]
  1-第二次创建。
  已删除地理位置
  列表包含:[1-第二]。