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

var componentArray: [String]

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

这将返回一个错误:'AnyObject'与string不相同

也试过

componentArray = dict.allKeys as String 

but get: 'String'不能转换为[String]


当前回答

斯威夫特和斯威夫特

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

componentArray = dict.allKeys // for NSDictionary

其他回答

在Swift 3中,Dictionary有一个keys属性。Keys有以下声明:

var keys: LazyMapCollection<Dictionary<Key, Value>, Key> { get }

仅包含字典键的集合。

注意,LazyMapCollection可以很容易地用Array的init(_:)初始化器映射到Array。


从NSDictionary到[String]

下面的iOS AppDelegate类片段展示了如何从NSDictionary中使用keys属性获得一个字符串数组([String]):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let string = Bundle.main.path(forResource: "Components", ofType: "plist")!
    if let dict = NSDictionary(contentsOfFile: string) as? [String : Int] {
        let lazyMapCollection = dict.keys
        
        let componentArray = Array(lazyMapCollection)
        print(componentArray)
        // prints: ["Car", "Boat"]
    }
    
    return true
}

从[String: Int]到[String]

下面的Playground代码以更通用的方式展示了如何使用keys属性从具有字符串键和整型值([String: Int])的字典中获得一个字符串数组([String]):

let dictionary = ["Gabrielle": 49, "Bree": 32, "Susan": 12, "Lynette": 7]
let lazyMapCollection = dictionary.keys

let stringArray = Array(lazyMapCollection)
print(stringArray)
// prints: ["Bree", "Susan", "Lynette", "Gabrielle"]

从[Int: String]到[String]

下面的Playground代码展示了如何从具有整型键和字符串值([Int: String])的字典中使用keys属性获得一个字符串数组([String]):

let dictionary = [49: "Gabrielle", 32: "Bree", 12: "Susan", 7: "Lynette"]
let lazyMapCollection = dictionary.keys
    
let stringArray = Array(lazyMapCollection.map { String($0) })
// let stringArray = Array(lazyMapCollection).map { String($0) } // also works
print(stringArray)
// prints: ["32", "12", "7", "49"]

NSDictionary是类(通过引用传递) 字典是结构(按值传递) ======数组从NSDictionary ======

NSDictionary有allKeys和allValues获取属性 类型(任何)。

  let objesctNSDictionary = 
    NSDictionary.init(dictionary: ["BR": "Brazil", "GH": "Ghana", "JP": "Japan"])
            let objectArrayOfAllKeys:Array = objesctNSDictionary.allKeys
            let objectArrayOfAllValues:Array = objesctNSDictionary.allValues
            print(objectArrayOfAllKeys)
            print(objectArrayOfAllValues)

======数组从字典======

字典的键和值属性的Apple引用。

let objectDictionary:Dictionary = 
            ["BR": "Brazil", "GH": "Ghana", "JP": "Japan"]
    let objectArrayOfAllKeys:Array = Array(objectDictionary.keys)          
    let objectArrayOfAllValues:Array = Array(objectDictionary.values)
    print(objectArrayOfAllKeys)
    print(objectArrayOfAllValues)

斯威夫特5

var dict = ["key1":"Value1", "key2":"Value2"]

let k = dict.keys

var a: [String]()
a.append(contentsOf: k)

这对我很有用。

// Old version (for history)
let keys = dictionary.keys.map { $0 }
let keys = dictionary?.keys.map { $0 } ?? [T]()

// New more explained version for our ducks
extension Dictionary {

    var allKeys: [Dictionary.Key] {
        return self.keys.map { $0 }
    }
}
extension Array {
    public func toDictionary<Key: Hashable>(with selectKey: (Element) -> Key) -> [Key:Element] {
        var dict = [Key:Element]()
        for element in self {
            dict[selectKey(element)] = element
        }
        return dict
    }
}