定义一个字典:
var dict = Dictionary<Key类型,value类型>() ——空字典!
或者 var dity:Dictionary= [“key1”:“www”,“key2”:“aaaa”,“key3”:“dddd”]
1.给字典中存储数据
dict.updateValue(value: Value, forKey: Key)
dict[2] = "B";
dict[3] = "C";
2.移除字典中的数据
dict.removeAll();
dict.removeAll(keepCapacity: true);
3.通过Key查找value元素
dict[Key]
4.遍历字典
///遍历字典的Key和Value
for (key,value) in dict {
print("\(key): \(value)")
}
///遍历字典的Key
for dictKeys in dict.keys {
print("dict.keys: \(dictKeys)")
}
///遍历字典的Value
for dictValues in dict.values {
print("dict.values: \(dictValues)")
}
网友评论