从AnyObject Response Swift获取价值

时间:2015-05-06 00:48:39

标签: ios swift

如何从服务器的响应中获取id,content,name的值。 来自服务器的响应是AnyObject,如果我打印,它看起来如下所示......

{
 content = xxxx
 id = 22
 name = yyyy
}

提前致谢。

1 个答案:

答案 0 :(得分:16)

AnyObject可以被转发到其他类型的类中,有很多可能性!

//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"]
}

结帐the reference。甚至还有一个关于AnyObject

的部分