[英]Swift 3 Value of type 'Any?' has no member 'object'
I have updated swift 3 and I found many errors. 我更新了swift 3,发现很多错误。 This is one of them : 这是其中之一:
Value of type 'Any?' 类型“任何吗?”的值 has no member 'object' 没有成员“对象”
This is my code : 这是我的代码:
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()
}
New Error Update : 新错误更新:
Optional chain has no effect, expression already produces 'Any?' 可选链无效,表达式已经产生“ Any?”
And 和
Cannot call value of non-function type 'Any?!' 无法调用非功能类型“ Any ?!”的值
It works well in previous version 7.3.1 swift 2. 在早期版本7.3.1 swift 2中运行良好。
This is json response : 这是json响应:
{
"meta":{"status":200,"msg":"OK"},
"response":[""],
"total_data":0
}
Unlike Swift 2, Swift 3 imports Objective-C's id
as Any?
与Swift 2不同,Swift 3会将Objective-C的id
导入为Any?
instead of AnyObject?
而不是AnyObject?
(see this Swift evolution proposal). (请参阅此 Swift进化建议)。 To fix your error, you need to cast all of your variables to AnyObject
. 要纠正错误,您需要将所有变量都AnyObject
为AnyObject
。 This may look something like the following: 这看起来可能类似于以下内容:
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()
}
}
您的responseObject
是Optional
(具体来说,是Any?
),因此您必须将其拆开以调用其方法或访问其属性,例如responseObject?.object(forKey: "meta")
等等。现在,那些以前是非Optional
值的框架现在是Optional
,尤其是在没有指定可空性限定符的情况下在Objective-C中使用它们的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.