我是 Xcode 和 Swift 新手,我正在尝试使用下面的代码从 url 序列化 JSON 数据并收到错误
'NSJSONReadingOptions' is not convertible to 'NSJSONWritingOptions'
上线
var jsonResult = NSJSONSerialization.dataWithJSONObject(data, options: NSJSONReadingOptions.MutableContainers, error: nil)!
我无法从在线资源中看到我的错误,其他人使用相同的代码及其工作原理?
let task = session.dataTaskWithURL(loginUrl!, completionHandler: { (data, response, error) -> Void in
if error != nil {
}else {
var jsonResult = NSJSONSerialization.dataWithJSONObject(data, options: NSJSONReadingOptions.MutableContainers, error: nil)!
println(jsonResult)
}
})
task.resume();
最佳答案
阅读 dataWithJSONObject
的文档。 options
参数必须是 NSJSONWritingOptions
枚举中的值,而不是 NSJSONReadingOptions
枚举。
但是,如果您的目标是将 NSData
对象转换为 NSArray
或 NSDictionary
,那么问题就在于您正在调用错误的方法。您想要使用 JSONObjectWithData
,而不是 dataWithJSONObject
。
关于Swift 中的 JSON 数据序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292943/