Search code examples
iosxcodeswift3ios-simulatorambiguous

Ambiguous use of subscript when attempting to use physical simulator


I am attempting to run my application on a physical device (iPhone 6+) and I keep getting this error message. Ambiguous use of subscript. When running the application on the simulator everything runs fine, I am wondering if it is an issue that corresponds to using a physical device.

//Making array to sort through the index of the specific field
        if let array = allUsers["user_info"] {
            for index in 0...array.count-1 {
                let aObject = array[index] as! [String : AnyObject]
                let Emails = aObject["email"] as! String
                let Passwords = aObject["password"] as! String
                user_info[Emails] = Passwords as AnyObject?
            }
        }

I am getting the error on the following line: let aObject = array[index] as! [String : AnyObject]

Image of error message within code.


Solution

  • That is because swift is not certain that your array in if let array = allUsers["user_info"] is an array . You can cast it like below and it should not complain:

    if let array = allUsers["user_info"] as? [AnyObject] {
        //you code
    }
    

    AnyObject would be the type of your array's content.