Get value from AnyObject Response Swift

How to get id, content, name values ​​from server response. The answer is from the server as AnyObject and if I type it looks like the one below ...

{
 content = xxxx
 id = 22
 name = yyyy
}

      

Thanks in advance.

+3


source to share


1 answer


AnyObject

can be omitted in other class types, so many possibilities!

//if you're confident that responseObject will definitely be of this dictionary type
let name = (responseObject as! [String : AnyObject])["name"] 

//optional dictionary type
let name = (responseObject as? [String : AnyObject])?["name"] 

//or unwrapping, name will be inferred as AnyObject
if let myDictionary = responseObject as? [String : AnyObject] {
    let name = myDictionary["name"]
}

//or unwrapping, name will be inferred as String
if let myDictionary = responseObject as? [String : String] {
    let name = myDictionary["name"]
}

      



Check the link . There's even a section onAnyObject

+13


source







All Articles