var namesOfIntegers = [Int: String]()
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
namesOfIntegers[16] = "sixteen"
airports["LHR"] = "London Heathrow"
// 更新值返回久的值,值可能为nil
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was \\(oldValue).")
}
if let x = dic[1]{
print(x)
}
namesOfIntegers = [:]
if airports.isEmpty {}
dic[1] = nil
dic.removeValue(forKey: 1)
for (airportCode, airportName) in airports {
print("\\(airportCode): \\(airportName)")
}
for airportCode in airports.keys {
print("Airport code: \\(airportCode)")
}
for airportName in airports.values {
print("Airport name: \\(airportName)")
}
var dic = [1:"23",3:"3"]
dic.remove(at: dic.index(dic.startIndex, offsetBy: 1))
print(dic)
运行:
[3: "3"]
let keys = [Int](dic.keys)
let values = [String](dic.values)
提示:
let keys = dic.keys 获取的不是数组,但是可以通过for...in遍历它里面的元素