2017-02-22 28 views
0

我有一个非常奇怪的错误。一直在学习Swift,并正在阅读有关在这里查找数组中对象索引的文档:https://developer.apple.com/reference/swift/array/1689674-indexindex(of:element)不存在

但是,这种方法在我的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我重视的出现并没有能够给方法的图像找到为什么这可能发生在任何地方的答案。

虽然我知道它存在,但我找不到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)

enter image description here

+0

这是您的文章中的错字?它应该是'items.index(of:rowitem)'。请注意'index'而不是'Index'。 – rmaddy

+0

'TaskItem'是否可以等值? – Hamish

+0

这不是,谢谢 – user2219097

回答

4

TaskItem需要符合Equatable协议才能使用index(of:)

0

我完全同意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{$0 === name}!)