스위프트 3 Dictionary에는 keys속성이 있습니다. keys다음과 같은 선언이 있습니다.
var keys: LazyMapCollection<Dictionary<Key, Value>, Key> { get }
사전의 키만 포함하는 컬렉션입니다.
참고 LazyMapCollection즉 쉽게 매핑 할 수 있습니다 Array와 Array의 init(_:)이니셜.
에서 NSDictionary로[String]
다음 iOS AppDelegate클래스 스 니펫은의 속성을 [String]사용하여 문자열 배열 ( ) 을 얻는 방법을 보여줍니다 .keysNSDictionary

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]
보다 일반적인 방법으로 다음 놀이터 코드는 문자열 키와 정수 값 ( )이 있는 사전에서 속성을 [String]사용하여 문자열 배열 ( ) 을 얻는 방법을 보여줍니다 .keys[String: Int]
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]
다음 놀이터 코드는 정수 키와 문자열 값 ( )이 있는 사전에서 속성을 [String]사용하여 문자열 배열 ( ) 을 얻는 방법을 보여줍니다 .keys[Int: 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"]