Kex
Kex

Reputation: 8589

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?

Upvotes: 0

Views: 2301

Answers (1)

Sam
Sam

Reputation: 159

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
}

Upvotes: 5

Related Questions