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

后端 未结 5 1263
抹茶落季
抹茶落季 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:15

    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.

    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
    ]
    

提交回复
热议问题