The index function is missing

When I try to print the index of the array that any object contains. This does not mean function indexOf

. When I try to manually enter my error I have added a screenshot.

But it works for String array. Can anyone help me solve this problem?

enter image description here

Also here I am showing my code

import UIKit

class ViewController: UIViewController {

    var arrayOfAnyObject: [Any] = [Any]()
    var arrayOfStringObject: [String] = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()


         print("Index of string object array \(arrayOfStringObject.index(of: "anyString")!)")

         print("Index of any object array \(arrayOfAnyObject.index(of: "anyObject")!)")




        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

      

+3


source to share


1 answer


You can either set the type you want in the array ( Int

, String

etc.), or convert arrayOfAnyObject

to AnyObject

before you do indexOf

, for example:



print("Index of any object array \((arrayOfAnyObject as AnyObject).index(of: "anyObject"))")

      

+4


source







All Articles