Swift:遍历字典数组
Swift: Looping through a Dictionary Array
我正在努力循环遍历从 Web 服务调用返回的字典值数组。
我已经实现了以下代码,但我似乎在 运行 上遇到了崩溃。
我还想将结果存储到自定义结构中。真的很难做到这一点,到目前为止这里的答案还没有奏效。如果有人能够提供帮助,将不胜感激。
let nudgesURLString = "http://www.whatthefoot.co.uk/NUDGE/nudges.php"
let nudgesURL = NSURL(string: nudgesURLString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(nudgesURL!, completionHandler: {data, response, error -> Void in
if error != nil {
println(error)
} else {
let nudgesJSONResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
let nudges: NSDictionary = nudgesJSONResult["nudges"] as NSDictionary
if let list = nudgesJSONResult["nudges"] as? [[String:String]] {
for nudgeDict in list {
let location = nudgeDict["location"]
println(location)
}
}
}
})
task.resume()
}
注意事项
此答案是使用 Swift 1.2 编写的,因此,根据您当前的 Swift 系统,可能需要对答案进行一些细微的文体和语法更改才能正常工作。
答案 -- Swift 1.2
这一行使您的代码崩溃:
let nudges: NSDictionary = nudgesJSONResult["nudges"] as NSDictionary
您正在强制转换 Swift 无法处理的问题。你永远不会进入你的 for 循环。
尝试将您的代码更改为更像这样:
let nudgesURLString = "http://www.whatthefoot.co.uk/NUDGE/nudges.php"
let nudgesURL = NSURL(string: nudgesURLString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(nudgesURL!, completionHandler: {data, response, error -> Void in
if error != nil {
println(error)
} else {
let nudgesJSONResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as [String : AnyObject]
if let nudges = nudgesJSONResult["nudges"] as? [[String : String]] {
for nudge in nudges {
let location = nudge["location"]
println("Got location: \(location)")
println("Got full nudge: \(nudge)")
}
}
}
})
task.resume()
谢谢,
我创建了以下存储数据的结构,还允许我在视图控制器中为特定索引创建字典。
struct NudgesLibrary {
var location: NSArray?
var message: NSArray?
var priority: NSArray?
var date: NSArray?
var nudges: NSArray?
init(nudgesObject: AnyObject) {
nudges = (nudgesObject["nudges"] as NSArray)
if let nudges = nudgesObject["nudges"] as? NSArray {
location = (nudges.valueForKey("location") as NSArray)
message = (nudges.valueForKey("message") as NSArray)
priority = (nudges.valueForKey("priority") as NSArray)
date = (nudges.valueForKey("date") as NSArray)
}
}
}
我正在努力循环遍历从 Web 服务调用返回的字典值数组。
我已经实现了以下代码,但我似乎在 运行 上遇到了崩溃。
我还想将结果存储到自定义结构中。真的很难做到这一点,到目前为止这里的答案还没有奏效。如果有人能够提供帮助,将不胜感激。
let nudgesURLString = "http://www.whatthefoot.co.uk/NUDGE/nudges.php"
let nudgesURL = NSURL(string: nudgesURLString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(nudgesURL!, completionHandler: {data, response, error -> Void in
if error != nil {
println(error)
} else {
let nudgesJSONResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
let nudges: NSDictionary = nudgesJSONResult["nudges"] as NSDictionary
if let list = nudgesJSONResult["nudges"] as? [[String:String]] {
for nudgeDict in list {
let location = nudgeDict["location"]
println(location)
}
}
}
})
task.resume()
}
注意事项
此答案是使用 Swift 1.2 编写的,因此,根据您当前的 Swift 系统,可能需要对答案进行一些细微的文体和语法更改才能正常工作。
答案 -- Swift 1.2
这一行使您的代码崩溃:
let nudges: NSDictionary = nudgesJSONResult["nudges"] as NSDictionary
您正在强制转换 Swift 无法处理的问题。你永远不会进入你的 for 循环。
尝试将您的代码更改为更像这样:
let nudgesURLString = "http://www.whatthefoot.co.uk/NUDGE/nudges.php"
let nudgesURL = NSURL(string: nudgesURLString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(nudgesURL!, completionHandler: {data, response, error -> Void in
if error != nil {
println(error)
} else {
let nudgesJSONResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as [String : AnyObject]
if let nudges = nudgesJSONResult["nudges"] as? [[String : String]] {
for nudge in nudges {
let location = nudge["location"]
println("Got location: \(location)")
println("Got full nudge: \(nudge)")
}
}
}
})
task.resume()
谢谢,
我创建了以下存储数据的结构,还允许我在视图控制器中为特定索引创建字典。
struct NudgesLibrary {
var location: NSArray?
var message: NSArray?
var priority: NSArray?
var date: NSArray?
var nudges: NSArray?
init(nudgesObject: AnyObject) {
nudges = (nudgesObject["nudges"] as NSArray)
if let nudges = nudgesObject["nudges"] as? NSArray {
location = (nudges.valueForKey("location") as NSArray)
message = (nudges.valueForKey("message") as NSArray)
priority = (nudges.valueForKey("priority") as NSArray)
date = (nudges.valueForKey("date") as NSArray)
}
}
}