ios – 如何从Swift中的NSDictionary获取单独的String数组中的所有键和值?
2020年01月16日
作者:佚名
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.我需要将所有键和值分成两个独立的数组?
基本上我想要这个
jsonresult.allkeys – >字符串数组
jsonresult.allvalues – >字符串数组
解决方法
您可以使用:
let keys = jsonResult.flatMap(){ $0.0 as? String } let values = jsonResult.flatMap(){ $0.1 }
以上是来客网为你收集整理的ios – 如何从Swift中的NSDictionary获取单独的String数组中的所有键和值?全部内容,希望文章能够帮你解决ios – 如何从Swift中的NSDictionary获取单独的String数组中的所有键和值?所遇到的程序开发问题。
如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。