Swift中的字典
By admin
- One minute read - 90 words//1.创建字典
var dict:Dictionary<Int, String> = [:“美国“,1:“中国“, 2:“英国“]
var country = [
“china”: “中国“,
“en”:“英国“,
“usa”:“美国“
]
//创建一个空的字典
var emptyDictionary = Dictionary<String, Float>()
var emptyDictionary2 = [:]
for (key,value) incountry {
var result = key + value
}
//2.获取字典中的所有键名和键值及字典元素个数
var keys = Array(country.keys)
var values = Array(country.values)
var cnum = country.count
//3.访问字典
var value = dict[]
var value2 = country[“china”]
//4.修改字典,修改后,字典中的位置会发生改变
country[“china”] = “中华人民共和国”
country.updateValue(“美国大兵“, forKey:“usa”)
for i incountry.values {
var name = i
}
//5.删除字典元素
country[“usa”] = nil
country.removeValueForKey(“en”)
for (key,value) incountry {
var result = key + value
}