ios Swift 3 'Any?' 类型的值 没有成员“对象”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39502476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:09:26  来源:igfitidea点击:

Swift 3 Value of type 'Any?' has no member 'object'

iosswiftswift3

提问by stevengbu

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'

'Any?' 类型的值 没有成员“对象”

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
}

回答by kabiroberai

Unlike Swift 2, Swift 3 imports Objective-C's idas Any?instead of AnyObject?(see thisSwift 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 的idasAny?而不是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() 
    }
}

回答by NRitH

Your responseObjectis 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-Optionalare now Optional, especially where they were used in Objective-C without a specified nullability qualifier.

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