Kex Kex - 2 months ago 126
Swift Question

Type Any has no subscript members Swift 3.0

Trying to convert a function to make it Swift 3.0 compatible. Had to change the parameter

json
from
AnyObject
to
Any
:

fileprivate func checkForAuthorizationFailure(_ json: Any) -> Bool {

let responseMessage = json["response"]! as? String
if responseMessage == "Unauthorized. Invalid token or email." {
return true
}

return false
}


However at the line:
let responseMessage = json["response"]! as? String
I am now getting the error: "Type Any has no subscript members". What am I doing wrong here?

Sam Sam
Answer

You have to cast Any to AnyObject before using subscript.

fileprivate func checkForAuthorizationFailure(_ json: Any) -> Bool {

    let responseMessage = (json as AnyObject)["response"]! as? String
    if responseMessage == "Unauthorized. Invalid token or email." {
        return true
    }

    return false
}
Comments