Swift의 Dictionary타입은 Foundation의 NSDictionary를 bridge한 타입.
빈 Dictionary 생성
var namesOfIntegers = [Int: String]()
namesOfIntegers[16] = "sixteen"
namesOfIntegers = [:]
리터럴로 Dictionary 생성
var airports: [String: String] = = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
Dictionary 접근, 변경
print("The airports dictionary contains \(airports.count) items.")
// 빈 Dictionary 확인
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
// 값 할당
airports["LHR"] = "London"
Array로부터 Grouping된 Dictionary 생성
let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
let studentsByLetter = Dictionary(grouping: students, by: { $0.first! })
print(studentsByLetter)
// ["K": ["Kofi", "Kweku"], "A": ["Abena", "Akosua"], "E": ["Efua"]]