【问题标题】:Cannot invoke 'indexOf' with an argument list of type '(ChecklistItem)'无法使用“(ChecklistItem)”类型的参数列表调用“indexOf”
【发布时间】:2015-08-06 09:45:17
【问题描述】:

当我编写代码以使用 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)
        }
    }

【问题讨论】:

  • 如果能把代码的引用放上就好了,我相信是iOS学徒这本书的第2章

标签: ios iphone xcode swift


【解决方案1】:

为了使用indexOfChecklistItem必须采用Equatable protocol。只有采用该协议,列表才能将一个项目与其他项目进行比较以找到所需的索引

【讨论】:

    【解决方案2】:

    indexOf 只能应用于Equatable 类型的集合,您的ChecklistItem 不符合Equatable 协议(具有== 运算符)。

    为了能够在全局范围内使用indexOf,请将其添加到包含ChecklistItem 类的文件中:

    func ==(lhs: ChecklistItem, rhs: ChecklistItem) -> Bool {
        return lhs === rhs
    }
    
    Swift3: 
    public static func ==(lhs: Place, rhs: Place) -> Bool {
            return lhs === rhs
        }
    

    请注意,它将通过比较内存中的实例地址来进行比较。您可能希望通过比较类的成员来检查相等性。

    【讨论】:

    • 还记得通过将 Equatable 添加到类定义中来使您的类符合 Equatable
    【解决方案3】:

    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
        }
    }
    

    用法:

    let index = self.photos.index(of: aPhoto)
    

    【讨论】:

      【解决方案4】:

      我意识到这个问题已经有一个可接受的答案,但我发现了另一个会导致此错误的案例,因此它可能对其他人有所帮助。我正在使用 Swift 3。

      如果您创建一个集合并允许推断类型,您也可能会看到此错误。

      例子:

      // 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
      ]
      

      【讨论】:

        【解决方案5】:

        可能的原因是你没有告诉 ChecklistItem 类型它是相等的,也许你忘了提到 ChecklistItem 类是继承自 NSObject强>。

        import Foundation
        
        class ChecklistItem: NSObject {
          var text = ""
          var checked = false
        
          func toggleChecked() {
            checked = !checked
          }
        }
        

        NSObject采用或符合可等价协议

        【讨论】:

        • 从 NSObject 继承是垃圾,应该尽快避免
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        相关资源
        最近更新 更多