enum StringEnum : String { case Lemon = "lemon" case Orange = "orange"}var swiftDictionary = [StringEnum: AnyObject]()swiftDictionary[.Lemon] = "string value"swiftDictionary[.Orange] = 123
当我尝试使用as关键字将其转换为NSDictionary时:
let objcDictionary: NSDictionary = swiftDictionary as NSDictionary
我收到编译器错误:
'[StringEnum : AnyObject]' is not convertible to 'NSDictionary'
可以转换这些类型的字典,还是需要循环Swift字典并手动创建NSDictionary?
解决方法 我想如果你的字典有枚举值,那么你不能将它转换为NSDictionary,但另一种方法是://change the type of your dict to [String: AnyObject]()var swiftDictionary = [String: AnyObject]()//you can store rawValue as a keyswiftDictionary[StringEnum.Lemon.rawValue] = "string value"swiftDictionary[StringEnum.Orange.rawValue] = 123let objcDictionary = swiftDictionary as NSDictionary //["lemon": "string value","orange": 123]
希望这会有所帮助.
总结以上是内存溢出为你收集整理的ios – 如何将带有枚举键的Swift词典转换为NSDictionary?全部内容,希望文章能够帮你解决ios – 如何将带有枚举键的Swift词典转换为NSDictionary?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)