有没有更好的方法在Swift中做这样的事情?有什么更好的方法来处理Swift中的JSON
var jsonError: NSError?
let jsonDict = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as NSDictionary
if jsonError != nil {
return
}
if let threadsArray = jsonDict["threads"] as? NSArray {
if let threadInfo = threadsArray[0] as? NSDictionary {
if let postsArray = threadInfo["posts"] as? NSArray {
if let opPostInfo = postsArray[0] as? NSDictionary {
if let filesArray = opPostInfo["files"] as? NSArray {
if let firstFileInfo = filesArray[0] as? NSDictionary {
if let thumbnail = firstFileInfo["thumbnail"] as? NSString {
// ...
}
}
}
}
}
}
}
我想你需要给我们一些更多的输入。一个更好的方式做什么? – mattias 2014-11-09 08:36:35
我想这个问题是:“我如何以一种更优雅的方式来处理可选项,而不是做所有那些讨厌的'如果'”? – 2014-11-09 08:38:08
你可以使用一个库。 https://github.com/owensd/json-swift看起来不错。 – cncool 2014-11-09 08:38:30