Swift 3'Any'类型的值没有会员'对象'

时间:2022-05-07 17:07:29

I have updated swift 3 and I found many errors. This is one of them :

我更新了swift 3,发现了很多错误。这是其中之一:

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?!'

无法调用非功能类型的值'任何?!'

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
}

2 个解决方案

#1


18  

Unlike Swift 2, Swift 3 imports Objective-C's id as Any? instead of AnyObject? (see this Swift evolution proposal). To fix your error, you need to cast all of your variables to AnyObject. This may look something like the following:

与Swift 2不同,Swift 3将Objective-C的id导入为Any?而不是AnyObject? (参见本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() 
    }
}

#2


0  

Your responseObject is Optional (specifically, an Any?), so you have to unwrap it in order to call its methods or access its properties, like responseObject?.object(forKey: "meta"), etc. There are several places in the frameworks where values that used to be non-Optional are now Optional, especially where they were used in Objective-C without a specified nullability qualifier.

你的responseObject是Optional(具体来说是一个Any?),所以你必须打开它才能调用它的方法或访问它的属性,比如responseObject?.object(forKey:“meta”)等。有几个地方在曾经是非可选值的框架现在是可选的,特别是在没有指定的可空性限定符的情况下在Objective-C中使用它们的框架。

#1


18  

Unlike Swift 2, Swift 3 imports Objective-C's id as Any? instead of AnyObject? (see this Swift evolution proposal). To fix your error, you need to cast all of your variables to AnyObject. This may look something like the following:

与Swift 2不同,Swift 3将Objective-C的id导入为Any?而不是AnyObject? (参见本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() 
    }
}

#2


0  

Your responseObject is Optional (specifically, an Any?), so you have to unwrap it in order to call its methods or access its properties, like responseObject?.object(forKey: "meta"), etc. There are several places in the frameworks where values that used to be non-Optional are now Optional, especially where they were used in Objective-C without a specified nullability qualifier.

你的responseObject是Optional(具体来说是一个Any?),所以你必须打开它才能调用它的方法或访问它的属性,比如responseObject?.object(forKey:“meta”)等。有几个地方在曾经是非可选值的框架现在是可选的,特别是在没有指定的可空性限定符的情况下在Objective-C中使用它们的框架。