xcode 无法使用类型为“(ChecklistItem)”的参数列表调用“indexOf”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31852359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot invoke 'indexOf' with an argument list of type '(ChecklistItem)'
提问by shahin ali agharia
When I am writing code for finding an item from the array with the use of indexOf it shows me the above stated error. Here is my code:-
当我编写代码以使用 indexOf 从数组中查找项目时,它向我显示了上述错误。这是我的代码:-
func addItemViewController(controller: AddItemViewController, didFinishEditingItem item: ChecklistItem)
{
if let index = items.indexOf(item)
{
let indexPath = NSIndexPath(forRow: index, inSection: 0)
if let cell = tableView.cellForRowAtIndexPath(indexPath)
{
configureTextForCell(cell, withChecklistItem: item)
}
}
回答by Zell B.
In order to use indexOf
the ChecklistItem
must adopt Equatable protocol. Only by adopting this protocol the list can compare an item with other items to find the desired index
为了使用indexOf
的ChecklistItem
,必须采取Equatable协议。只有采用此协议,列表才能将一个项目与其他项目进行比较以找到所需的索引
回答by Fantattitude
indexOf
can only be applied to Collections of Equatable
types, your ChecklistItem
doesn't conform to Equatable
protocol (have an ==
operator).
indexOf
只能应用于Equatable
类型的集合,你ChecklistItem
不符合Equatable
协议(有一个==
运算符)。
To be able to use indexOf
add this to the file containing ChecklistItem
class in the global scope :
为了能够使用indexOf
将其添加到ChecklistItem
全局范围内包含类的文件中:
func ==(lhs: ChecklistItem, rhs: ChecklistItem) -> Bool {
return lhs === rhs
}
Swift3:
public static func ==(lhs: Place, rhs: Place) -> Bool {
return lhs === rhs
}
Please note it will make comparison by comparing instances addresses in memory. You may want to check equality by comparing members of the class instead.
请注意,它将通过比较内存中的实例地址来进行比较。您可能希望通过比较类的成员来检查相等性。
回答by Naishta
In Swift 4 and Swift 3, update your Data Model to conform to "Equatable" protocol, and implement the lhs=rhs method , only then you can use ".index(of:...)", because you are comparing your custom object
在Swift 4 和 Swift 3 中,更新您的数据模型以符合“Equatable”协议,并实现 lhs=rhs 方法,只有这样您才能使用“.index(of:...)”,因为您正在比较您的自定义目的
Eg:
class Photo : Equatable{
var imageURL: URL?
init(imageURL: URL){
self.imageURL = imageURL
}
static func == (lhs: Photo, rhs: Photo) -> Bool{
return lhs.imageURL == rhs.imageURL
}
}
Usage:
用法:
let index = self.photos.index(of: aPhoto)
回答by digitalHound
I realize this question already has an accepted answer, but I found another case that will cause this error so it might help someone else. I'm using Swift 3.
我意识到这个问题已经有一个公认的答案,但我发现了另一个会导致这个错误的案例,所以它可能会帮助其他人。我正在使用 Swift 3。
If you create a collection and allow the type to be inferred you may also see this error.
如果您创建一个集合并允许推断类型,您也可能会看到此错误。
Example:
例子:
// UITextfield conforms to the 'Equatable' protocol, but if you create an
// array of UITextfields and leave the explicit type off you will
// also see this error when trying to find the index as below
let fields = [
tf_username,
tf_email,
tf_firstname,
tf_lastname,
tf_password,
tf_password2
]
// you will see the error here
let index = fields.index(of: textField)
// to fix this issue update your array declaration with an explicit type
let fields:[UITextField] = [
// fields here
]
回答by SLN
The possible reason is you didn't tell the ChecklistItemtype that it is equatable, maybe you forgot to mention ChecklistItemclass is inherited from NSObject.
可能的原因是你没有告诉ChecklistItem类型它是 equatable 的,也许你忘了提到ChecklistItem类是从NSObject继承的。
import Foundation
class ChecklistItem: NSObject {
var text = ""
var checked = false
func toggleChecked() {
checked = !checked
}
}
NSObjectadopts or conforms to the equatable protocol
NSObject采用或符合 equatable 协议