数组从字典键swift [英] Array from dictionary keys in swift

查看:159
本文介绍了数组从字典键swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var componentArray:[String] 

let dict = NSDictionary(contentsOfFile:NSBundle.mainBundle()。pathForResource(Components,ofType:plist)!)
componentArray = dict.allKeys

这会返回一个错误:'AnyObject'与字符串不相同



还试过

  componentArray = dict.allKeys as String 

但得到:'String'不能转换为[String]

解决方案

Swift 3

  componentArray = Array(dict.keys)// for Dictionary 

componentArray = dict。 allKeys // for NSDictionary


Trying to fill an array with strings from the keys in a dictionary in swift.

var componentArray: [String]

let dict = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("Components", ofType: "plist")!)
componentArray = dict.allKeys

This returns an error of: 'AnyObject' not identical to string

Also tried

componentArray = dict.allKeys as String 

but get: 'String' is not convertible to [String]

解决方案

Swift 3

componentArray = Array(dict.keys) // for Dictionary

componentArray = dict.allKeys // for NSDictionary

这篇关于数组从字典键swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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