Type Any没有下标成员Swift 3.0

时间:2016-09-22 14:21:39

标签: ios swift

尝试转换函数以使其与Swift 3.0兼容。必须将参数jsonAnyObject更改为Any

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

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

        return false
    }

但是在第let responseMessage = json["response"]! as? String行:我现在收到错误: “Type Any没有下标成员” 。我在这里做错了什么?

1 个答案:

答案 0 :(得分:5)

在使用下标之前,必须将Any转换为AnyObject。

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
}