本文共 8604 字,大约阅读时间需要 28 分钟。
public struct Dictionary: CollectionType, DictionaryLiteralConvertible public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration
let dictinoary:[String:String] = ["key1":"value1", "key2":"value"] // Dictionary 转 NSDictionary // Dictionary 转换成 NSDictionary型 let nsDictionary1:NSDictionary = dictinoary // Dictionary 转换成 NSDictionary 型 let nsDictionary2:NSDictionary = dictinoary as NSDictionary // NSDictionary 转 Dictionary // NSDictionary 转换成 Dictionary 型 let swiftDictionary1:Dictionary = nsDictionary1 as Dictionary // NSDictionary 转换成 Dictionary 型 let swiftDictionary2:Dictionary = nsDictionary1 as! [String:String]
// Dictionary 型字典 // 创建字典变量,指定数据类型为 Int 型 let dic1 = [String:Int]() // 创建字典变量,Dictionary等价于 [String:Int] let dic2 = Dictionary () // 创建字典变量,不指定数据类型 let dic3:Dictionary = ["key1":1, "key2":2] // 创建字典变量,指定数据类型为 Int 型 let dic4:Dictionary = ["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"))
// Dictionary 或 NSDictionary 型字典 let dic = ["key1":1, "key2":2, "key3":3, "key4":4] let num:Int = dic.count
// 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")
// NSDictionary 型字典 let nsDic:NSDictionary = NSDictionary(dictionary: ["key1":1, "key2":3, "key3":3, "key4":4]) let keys:AnyObject = nsDic.allKeysForObject(3)
// 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
// 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
// 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])
// 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])
// 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()
// 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) 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) }
// 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)) }
转载地址:http://pttrl.baihongyu.com/