Cannot invoke 'indexOf' with an argument list of type '(ChecklistItem)'

后端 未结 5 1255
抹茶落季
抹茶落季 2020-12-10 01:36

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

func addItemViewContr         


        
5条回答
  •  北海茫月
    2020-12-10 02:01

    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

    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)
    

提交回复
热议问题