我正在使用下面的代码进行 json 解析,但编译器提示警告“Constant json inferred to have 'AnyObject', which may be unexpected”。我将如何解决警告?我的 json 响应可能是 json 数组或字典。如何以通用方式实现它?
if let json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &jsonError) {
}
最佳答案
试试这个:
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
}
关于swift - 常量 json 推断有 'AnyObject' ,这可能是意外的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27291629/