如何从Swift中的NSDictionary获取单独的String数组中的所有键和值? [英] How to get all keys and values into separate String arrays from NSDictionary in Swift?
本文介绍了如何从Swift中的NSDictionary获取单独的String数组中的所有键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
let urlAsString = "https://drive.google.com/uc?export=download&id=0B2bvUUCDODywWTV2Q2IwVjFaLW8"
let url = NSURL(string: urlAsString)!
let urlSession = NSURLSession.sharedSession()
let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
do {
if let jsonDate = data, let jsonResult = try NSJSONSerialization.JSONObjectWithData(jsonDate, options: []) as? NSDictionary {
print(jsonResult)
}
} catch let error as NSError {
print(error)
}
})
jsonQuery.resume()
好的,我在这里接收来自在线json的数据,然后将其存储为jsonresult中的NSDictionary。我需要将所有键和值分成两个独立的数组?
Okay so here i am receiving data from online json then storing it as NSDictionary in jsonresult . I need to get all keys and values as into two separate arrays ?
基本上我想要这个
jsonresult.allkeys - > String array
jsonresult.allvalues - > String array
推荐答案
你可以使用:
let keys = jsonResult.flatMap(){ $0.0 as? String }
let values = jsonResult.flatMap(){ $0.1 }
这篇关于如何从Swift中的NSDictionary获取单独的String数组中的所有键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文