ios – 如何从Swift中的NSDictionary获取单独的String数组中的所有键和值?

前端之家收集整理的这篇文章主要介绍了ios – 如何从Swift中的NSDictionary获取单独的String数组中的所有键和值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. let urlAsString = "https://drive.google.com/uc?export=download&id=0B2bvUUCDODywWTV2Q2IwVjFaLW8"
  2. let url = NSURL(string: urlAsString)!
  3. let urlSession = NSURLSession.sharedSession()
  4.  
  5. let jsonQuery = urlSession.dataTaskWithURL(url,completionHandler: { data,response,error -> Void in
  6. do {
  7. if let jsonDate = data,let jsonResult = try NSJSONSerialization.JSONObjectWithData(jsonDate,options: []) as? NSDictionary {
  8.  
  9. print(jsonResult)
  10.  
  11. }
  12. } catch let error as NSError {
  13. print(error)
  14. }
  15.  
  16.  
  17. })
  18. jsonQuery.resume()

好的,我在这里接收来自在线json的数据,然后将其存储为jsonresult中的NSDictionary.我需要将所有键和值分成两个独立的数组?

基本上我想要这个

jsonresult.allkeys – >字符串数组
jsonresult.allvalues – >字符串数组

解决方法

您可以使用:
  1. let keys = jsonResult.flatMap(){ $0.0 as? String }
  2. let values = jsonResult.flatMap(){ $0.1 }

猜你在找的iOS相关文章