Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

swift3

I have updated swift 3 and I found many errors. 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?'

And

Cannot call value of non-function type 'Any?!'

It works well in previous version 7.3.1 swift 2.

This is json response :

{
 "meta":{"status":200,"msg":"OK"},
        "response":[""],
        "total_data":0
}
like image 380
stevengbu Avatar asked Sep 15 '16 02:09

stevengbu


1 Answers

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.

like image 184
NRitH Avatar answered Oct 08 '22 09:10

NRitH