ios – Swift 3’Any’类型的值没有会员’对象’

前端之家收集整理的这篇文章主要介绍了ios – Swift 3’Any’类型的值没有会员’对象’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我更新了 swift 3,发现了很多错误.这是其中之一:

Value of type ‘Any?’ has no member ‘object’

这是我的代码

jsonmanager.post( "http://myapi.com",parameters: nil,success: { (operation: AFHTTPRequestOperation?,responSEObject: Any?) in
                        if(((responSEObject? as AnyObject).object(forKey: "Meta") as AnyObject).object(forKey: "status")?.intValue == 200 && responSEObject?.object(forKey: "total_data")?.intValue > 0){
                            let aa: Any? = (responSEObject? as AnyObject).object(forKey: "response")

                            self.data = (aa as AnyObject).mutableCopy() 
                        }

错误更新:

Optional chain has no effect,expression already produces ‘Any?’

Cannot call value of non-function type ‘Any?!’

它在以前的版本7.3.1 swift 2中运行良好.

这是json的回应:

{
 "Meta":{"status":200,"msg":"OK"},"response":[""],"total_data":0
}

解决方法

与Swift 2不同,Swift 3将Objective-C的id导入为Any?而不是AnyObject? (见 this Swift进化提案).要修复错误,需要将所有变量强制转换为AnyObject.这可能类似于以下内容
jsonmanager.post("http://myapi.com",parameters: nil) { (operation: AFHTTPRequestOperation?,responSEObject: Any?) in
    let response = responSEObject as AnyObject?
    let Meta = response?.object(forKey: "Meta") as AnyObject?
    let status = Meta?.object(forKey: "status") as AnyObject?
    let totalData = response?.object(forKey: "total_data") as AnyObject?
    if status?.intValue == 200 && totalData?.intValue != 0 {
        let aa = response?.object(forKey: "response") as AnyObject?
        self.data = aa?.mutableCopy() 
    }
}

猜你在找的iOS相关文章