常量json推断为具有' AnyObject',这可能是意外的

时间:2014-12-04 10:28:15

标签: swift

我正在使用下面的代码进行json解析但是编译器抱怨警告“常量json推断为'AnyObject',这可能是意外的”。我该如何解决警告?我的json响应可能是json数组或字典。如何以共同的方式实现它?

if let json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &jsonError) {
}

1 个答案:

答案 0 :(得分:1)

试试这个:

let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: nil)
if let j = json as? Array<AnyObject> {
    //this is an array
} else if let j = json as? Dictionary<String, AnyObject> {
    //this is a dict
}