Array from dictionary keys in swift

Kyle Goslan picture Kyle Goslan · Oct 15, 2014 · Viewed 176.6k times · Source

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]

Answer

Andrius Steponavičius picture Andrius Steponavičius · Feb 22, 2015

Swift 3 & Swift 4

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

componentArray = dict.allKeys // for NSDictionary