Swift 字典 Dictionary 集合类型

Swift 字典 Dictionary 集合类型,第1张

Swift 字典 Dictionary 集合类型 Swift 字典 Dictionary 集合类型

Dictionary 字典集合类型。类似于 Java 的 map。特点:键唯一,值可重复

1. 创建字典
// 创建空字典
var emptyDict1: [Int : String] = [:]
var emptyDict2: Dictionary = Dictionary()

// 指定键值类型
var dict: [Int : String] = [0: "Zero", 1: "One", 2: "Two"]
var dict2: Dictionary = Dictionary(dictionaryLiteral: (1, "One"), (2, "Two"))

// 自动推断键值类型
var dictAuto = [0: "Zero", 1: "One", 2: "Two"]

2. 获取元素
// 获取元素个数
dict.count // 3

// 判断字典为空
dict.isEmpty // false

// 获取键对应的值
dict[1] // One

3. 更新键值对
// 修改键对应的值
dict[0] = "000" // 000

// 如果键存在,就更新,否则就新增键值对
dict[-1] = "-1" // -1

// 更新键对应的值,如果键不存在,返回 nil
if let lastValue = dict.updatevalue("new one", forKey: 1) {
    print("the last value is (lastValue)") // the last value is One
}

// 移除键值对
dict.removevalue(forKey: -1) // -1

// 移除所有键值对
//dict.removeAll()

4. 遍历字典
// dict [1: "new one", 2: "Two", 0: "000"]

// 遍历字典的键
for ele in dict.keys {
    print(ele)
}

// 遍历字典的值
for ele in dict.values {
    print(ele)
}

// 元组遍历,直接获取键值对
for (key, val) in dict {
    print("(key):(val)")
}

// 对 key 进行从小到大排序后遍历,并对值进行拆包
for ele in dict.keys.sorted(by: <) {
    print(dict[ele]!)
}
GitHub 源码:DictionaryType.playground

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5162722.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存