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

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

问题描述

尝试用 swift 字典中键的字符串填充数组.

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

这将返回错误:'AnyObject' 与字符串不相同

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

也试过了

componentArray = dict.allKeys as String 

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

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

推荐答案

Swift 3 &斯威夫特 4

Swift 3 & Swift 4

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

componentArray = dict.allKeys // for NSDictionary

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

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