关于ios:Type Any没有下标成员Swift 3.0

Type Any has no subscript members Swift 3.0

试图转换功能使其与Swift 3.0兼容。必须将参数jsonAnyObject更改为Any

1
2
3
4
5
6
7
8
9
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没有下标成员"。我在这里做错什么了吗?


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

1
2
3
4
5
6
7
8
9
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
}