索引(属于:元素)不存在

index (of: element) does not exist

我有一个非常奇怪的错误。一直在学习 Swift 并在此处阅读有关在数组中查找对象索引的文档:https://developer.apple.com/reference/swift/array/1689674-index

但是我的 Xcode 中不存在这种方法。

这是一个例子:

var items : [TaskItem]
items = [TaskItem] () 

    let rowitem = TaskItem()
    rowitem.text = "helloworld"
    items.append(rowitem)

//Attempting to find the index of an object

   items.Index(of: rowitem) 

//错误 - Xcode 无法找到我要查找的特定函数

我附上了出现的方法的图像,但无法找到任何地方可能发生这种情况的原因的答案。

我找不到 func index(of:) 方法的用法,虽然我知道它存在!

我收到以下编译器错误:

ChecklistViewController.swift:186:26: Argument labels '(of:, _:)' do not match any available overloads

ChecklistViewController.swift:186:26: Overloads for 'index' exist with these partially matching parameter lists: (Int, offsetBy: Int), (Self.Index, offsetBy: Self.IndexDistance)

TaskItem 需要遵守 Equatable 协议才能使用 index(of:).

我完全同意 Tim vermeulen 的观点,但我们可以使用下面的代码获取数组的索引并且它的工作非常完美。

class Name {

    var firstName:String  = String()
    var lastName:String = String()

    var fullName:String{
        return firstName+lastName
    }
}

var items:[Name] = [Name]()

    let rowitem = Name()

    let name:Name = Name()
    name.firstName = "Vala"
    name.lastName = "bbbb"

    items.append(rowitem)
    items.append(name)

//Attempting to find the index of an object
   print(items.index{[=10=] === name}!)