简体   繁体   中英

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 let responseMessage = json["response"]! as? String let responseMessage = json["response"]! as? String I am now getting the error: "Type Any has no subscript members" . What am I doing wrong here?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM