前言
public struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible
public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration
- 字典用于存储无序的数据元素值,它可以存储任何类型的元素,甚至可以将数组作为字典的元素。
1、Dictionary 与 NSDictionary 的相互转换
- Dictionary 与 NSDictionary 之间可以直接相互转换。Dictionary 转转成 NSDictionary 时,数组中的各项元素被转换为 AnyObject 类型。NSDictionary 转换成 Dictionary 时,数组中的各项元素也被转换为 AnyObject 类型。
let dictinoary:[String:String] = ["key1":"value1", "key2":"value"]
// Dictionary 转 NSDictionary
// Dictionary 转换成 NSDictionary<AnyObject, AnyObject> 型
let nsDictionary1:NSDictionary = dictinoary
// Dictionary 转换成 NSDictionary<AnyObject, AnyObject> 型
let nsDictionary2:NSDictionary = dictinoary as NSDictionary
// NSDictionary 转 Dictionary
// NSDictionary 转换成 Dictionary<AnyObject, AnyObject> 型
let swiftDictionary1:Dictionary = nsDictionary1 as Dictionary
// NSDictionary 转换成 Dictionary<String, String> 型
let swiftDictionary2:Dictionary = nsDictionary1 as! [String:String]
2、字典的 创建
// Dictionary 型字典
// 创建字典变量,指定数据类型为 Int 型
let dic1 = [String:Int]()
// 创建字典变量,Dictionary<String, Int> 等价于 [String:Int]
let dic2 = Dictionary<String, Int>()
// 创建字典变量,不指定数据类型
let dic3:Dictionary = ["key1":1, "key2":2]
// 创建字典变量,指定数据类型为 Int 型
let dic4:Dictionary<String, Int> = ["key1":1, "key2":2, "key3":3]
// 创建字典变量,不指定数据类型
let dic5 = ["key1":1, "key2":2, "key3":3, "key4":4]
// 创建指定数据类型的字典
let dic6:[String:Int] = ["key1":1, "key2":2, "key3":3, "key4":4]
// 创建指定数据类型的字典
let dic7:[String:AnyObject] = ["key1":1, "key2":2, "key3":"value3", "key4":4]
// NSDictionary 型字典
let swiftDictionary = ["key1":1, "key2":2, "key3":3, "key4":4]
let nsdic1:NSDictionary = swiftDictionary
let nsdic2:NSDictionary = swiftDictionary as NSDictionary
let nsdic3:NSDictionary = NSDictionary(dictionary: swiftDictionary)
let nsdic4:NSDictionary = NSDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
let nsdic5:NSDictionary = NSDictionary(object: "nsdic5", forKey: "key1")
let nsdic6:NSDictionary = NSDictionary(objects: ["nsdic6", 2], forKeys: ["key1", "key2"])
let nsdic7:NSDictionary = NSDictionary(dictionaryLiteral: ("key1", "nsdic7"),
("key2", 2),
("key3", 3),
("key4", 4))
// 从 文件 创建字符串
let nsdic8:NSDictionary? = NSDictionary(contentsOfFile: NSHomeDirectory() + "/Desktop/test.txt")
// 从 Url 创建字符串
let nsdic9:NSDictionary? = NSDictionary(contentsOfURL: NSURL(fileURLWithPath: NSHomeDirectory() +
"/Desktop/test.txt"))
3、字典元素个数的计算
// Dictionary 或 NSDictionary 型字典
let dic = ["key1":1, "key2":2, "key3":3, "key4":4]
let num:Int = dic.count
4、通过 key 找对应的 value
// Dictionary 型字典
var dic:Dictionary = ["key1":1, "key2":2, "key3":3, "key4":4]
// 获取字典中指定 key 对应的值
let value1 = dic["key2"]
// NSDictionary 型字典
let nsDic:NSDictionary = NSDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
// 获取字典中指定 key 对应的值
let value2:AnyObject? = nsDic["key2"]
// 获取字典中指定 key 对应的值
let value3:AnyObject? = nsDic.objectForKey("key2")
// 获取字典中指定 key 对应的值,key 的值不存在时返回指定的字符串
let value4 = nsDic.objectsForKeys(["key3", "key4", "key5"], notFoundMarker: "not found")
5、通过 value 找对应的 key
// NSDictionary 型字典
let nsDic:NSDictionary = NSDictionary(dictionary: ["key1":1, "key2":3, "key3":3, "key4":4])
let keys:AnyObject = nsDic.allKeysForObject(3)
6、字典中所有 key 的获取
// Dictionary 型字典
let dic:Dictionary = ["key1":1, "key2":2, "key3":3, "key4":4]
let keys1 = dic.keys
// NSDictionary 型字典
let nsDic:NSDictionary = NSDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
let keys2 = nsDic.allKeys
7、字典中所有 value 的获取
// Dictionary 型字典
let dic:Dictionary = ["key1":1, "key2":2, "key3":3, "key4":4]
let values1 = dic.values
// NSDictionary 型字典
let nsDic:NSDictionary = NSDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
let values2 = nsDic.allValues
8、字典键值对的 追加
// Dictionary 型字典
var dic:Dictionary = ["key1":1, "key2":2, "key3":3, "key4":4]
// key 不存在时,追加键值对(key 值为新增的,若 key 值已存在则为修改对应的 value 值)
dic["key5"] = 5
// NSDictionary 型字典
let nsDic:NSMutableDictionary = NSMutableDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
// key 不存在时,追加键值对(key 值为新增的,若 key 值已存在则为修改对应的 value 值)
nsDic["key5"] = 5
// 向字典末尾追加一个字典
nsDic.addEntriesFromDictionary(["key6":6, "key7":7])
9、字典键值对的 修改
// Dictionary 型字典
var dic:Dictionary = ["key1":1, "key2":2, "key3":3, "key4":4]
// key 存在时,修改其值
dic["key4"] = 100
// 修改指定的 key 对应的值
dic.updateValue(44, forKey: "key4")
// NSDictionary 型字典
let nsDic:NSMutableDictionary = NSMutableDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
// key 存在时,修改其值
nsDic["key3"] = 33
// 修改指定的 key 对应的值
nsDic.setObject(100, forKey: "key4")
// 修改整个字典
nsDic.setDictionary(["key6":6, "key7":7])
10、字典键值对的 删除
// Dictionary 型字典
var dic:Dictionary = ["key1":1, "key2":2, "key3":3, "key4":4]
// 删除指定 key 对应的值
dic.removeValueForKey("key2")
// 删除字典的所有元素
dic.removeAll(keepCapacity: true)
// NSDictionary 型字典
let nsDic:NSMutableDictionary = NSMutableDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
// 删除指定 key 对应的值
nsDic.removeObjectForKey("key2")
// 删除指定 key 对应的值
nsDic.removeObjectsForKeys(["key1", "key4"])
// 删除字典的所有元素
nsDic.removeAllObjects()
11、字典遍历
// NSDictionary 型字典
let nsDic:NSDictionary = NSDictionary(dictionary: ["key1":1, "key2":2, "key3":3, "key4":4])
// for...in 循环
// 遍历出的为 key-value 的元组, key 不需要可以用 _ 替代
for (_, obj) in nsDic {
print(obj)
}
// 闭包遍历
nsDic.enumerateKeysAndObjectsUsingBlock { (key:AnyObject, obj:AnyObject, stop:UnsafeMutablePointer<ObjCBool>) in
print(obj)
}
// 迭代器遍历
// key 迭代器
let keyEnu:NSEnumerator = nsDic.keyEnumerator()
while let key:AnyObject = keyEnu.nextObject() {
print(nsDic.objectForKey(key))
}
// 对象迭代器
let objEnu:NSEnumerator = nsDic.objectEnumerator()
while let obj:AnyObject = objEnu.nextObject() {
print(obj)
}
12、字典排序
// NSDictionary 型字典
let nsDic:NSDictionary = NSDictionary(dictionary: ["key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4"])
// 用闭包排序
let keys1 = nsDic.keysSortedByValueUsingComparator { (obj1:AnyObject, obj2:AnyObject) -> NSComparisonResult in
return (obj1 as! String).compare(obj2 as! String)
}
for key in keys1 {
print(nsDic.objectForKey(key))
}
// 用指定的方法排序
let keys2 = nsDic.keysSortedByValueUsingSelector(#selector(NSNumber.compare(_:)))
for key in keys2 {
print(nsDic.objectForKey(key))
}