将NSDictionary转换为Swift Dictionary [英] Convert NSDictionary to Swift Dictionary

查看:134
本文介绍了将NSDictionary转换为Swift Dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我知道,当swift编译它只是一个NSDictionary,但NSDictionary和Swift字典有不同的语法。有一种方法(通过一个循环或某事)将NSDictionary转换为相同类型的快捷字典,用于< key,value>





有没有办法将其转换为Swift字典而不是NSDictionary?

  let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData,options:nil,error:& error)as NSDictionary 


解决方案

使用:

 让jsonDic = NSJSONSerialization.JSONObjectWithData(jsonData,options:NSJSONReadingOptions.MutableContainers,error:& error)as Dictionary< String,AnyObject> ;; 


Now I know that when swift compiles it just makes a NSDictionary, but the NSDictionary and Swift dictionaries have different syntax. Is there a way (through a loop or something) to convert a NSDictionary to a swift dictionary of the same type for <key, value>?

OR

Is there a way to convert this to a Swift dictionary instead of NSDictionary?

let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary

解决方案

use:

let jsonDic = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: &error) as Dictionary<String, AnyObject>;

这篇关于将NSDictionary转换为Swift Dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆