【Swift4.0】集合类型-Dictionary
来源:互联网 发布:模拟电子琴软件下载 编辑:程序博客网 时间:2024/06/06 20:38
翻译能力有限,如有不对的地方,还请见谅!希望对Swift的学习者有所帮助,使用的编写工具:JQNote
Dictionary是一个字典,存储的是键值对,也是无序的。每一个值对应唯一的key。写作Dictionary<Key, Value>, 其中key是Dictionary中一个键的类型,Value是Dictionary中与键对应的存储值的类型。Dictionary简写形式为[Key : Value]
与集合类型Set类似,Dictionary中的Key类型必须遵守Hashable协议
创建一个空的Dictionary
跟Array一样,你可以使用初始化语法创建一个空Dictionary:
var namesOfIntegers = [Int :String] ()
这个例子创建了一个空的[Int : String]字典类型,它的Key是Int类型,Value是String类型。如果代码上下文已经提供了类型信息,那么可以创建一个空的Dictionary,使用[ : ].
namesOfIntegers[16] = “Sixteen”
namesOfIntegers = [:].
也可以这样创建一个Dictionary:
var airports: [String:String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
airports声明为一个[String:String]类型的Dictionary,也就是说,它的key是String类型,它的Value也是String类型。
与Array一样,如果初始化值的类型是明确的,那么也可以不用写[String: String]:
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
因为初始化值每一个key都是String,对应每个Value也都是String,Swift会自动推断该Dictionary的类型为[String:String].
获取和修改Dictionary
跟Array一样,你可以使用Dictionary的只读属性count来检查它的存储项数量
print("The airports dictionary contains \(airports.count) items.")
// Prints "The airports dictionary contains 2 items.
使用isEmpty属性来判断是否count属性为0
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
// Prints "The airports dictionary is not empty.”
使用下标索引语法来添加一个合适类型的key以及对应的值,
airports["LHR"] ="London"
或者改变一个值
airports["LHR"] ="London Heathrow” “// the value for "LHR" has been changed to "London Heathrow”
也可以使用updateValue(_:forKey:)方法来更新某个特定key对应的值, 如果不存在,会添加一个,如果存在,则更新。并且返回原来的旧值。
if letoldValue =airports.updateValue("Dublin Airport",forKey:"DUB") {
print("The old value for DUB was \(oldValue).")
}
// Prints "The old value for DUB was Dublin.”
也可以使用下标索引:
if let airportName=airports["DUB"]{
print("The name of the airport is\(airportName).")
} else {
print("That airport is not in the airports dictionary.")
}
// Prints "The name of the airport is Dublin Airport.”
删除一个键值对:
airports["APL"]="Apple International"
// "Apple International" is not the real airport for APL, so delete it
airports["APL"] =nil
// APL has now been removed from the dictionary”
或者使用removeValue(forKey:)
if letremovedValue =airports.removeValue(forKey:"DUB") {
print("The removed airport's name is\(removedValue).")
} else {
print("The airports dictionary does not contain a value for DUB.")
}
// Prints "The removed airport's name is Dublin Airport.”
遍历Dictionary
可以使用for-in循环来遍历一个Dictionary,每一项返回一个(key,value)元组
for (airportCode,airportName)inairports {
print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow
如果想获取Dictionary中所有的key,或者所有的value,可以使用它的keys和values属性:
let airportCodes= [String](airports.keys)
// airportCodes is ["YYZ", "LHR"]
let airportNames=[String](airports.values)
// airportNames is ["Toronto Pearson", "London Heathrow"]
最后,Dictionary是无序的,如果想有序的遍历一个Dictionary,用sorted()方法在它的keys和values属性。
- 【Swift4.0】集合类型-Dictionary
- 【Swift4.0】集合类型-Array
- 【Swift4.0】集合类型-Set
- Swift4.0 guard,Array,Dictionary
- 【Swift4.0】可选类型
- 【Swift4.0】类型安全和类型推断
- Swift4.0新特性之String、Array和Dictionary
- Hashtable 和 Dictionary 集合类型
- Swift4学习笔记4——集合类型
- swift3.0集合类型(Array,Set,Dictionary)
- Swift4.0【Optional String 类型 强制拆包】
- Dictionary集合
- C#.NET集合类型Hashtable与Dictionary 的区别
- Swift中文教程 -- 集合类型(Array,Dictionary)
- Swift语法_集合类型(Array.Set. Dictionary)
- swift4.0 涂鸦画板
- swift4.0 UIButton小解
- swift4.0 新特性
- 浅谈自己理解的socket(2)
- Springboot教程六——项目信息
- hibernate数据类型之间的映射关系
- Android.mk
- 你真的了解Android Notification吗?
- 【Swift4.0】集合类型-Dictionary
- git subtree 拆库,提交,建立关联
- Python入门
- nginx
- spring IOC
- solr删除数据的4种方便快捷的方式
- Unity方法传递方法类型的参数。(Unity萌新的备忘录)
- 深度学习|神经网络模型求解思路总结
- 流式布局 点击事件