Swift字典类
在Foundation框架中提供一种字典集合,它是由“键-值”对构成的集合。键集合不能重复,值集合没有特殊要求。键和值集合中的元素可以是任何对象,但是不能是nil。Foundation框架字典类也分为NSDictionary不可变字典和NSMutableDictionary可变字典。
一、NSDictionary类
NSDictionary有很多方法和属性,下面总结其常用的方法和属性。
initWithDictionary: 构造器,通过Swift的Dictionary创建NSDictionary对象。
initWithObjects:forKeys: 构造器,通过键集合和值集合创建NSDictionary对象。
count 字典集合的长度。
objectForKey: 通过键获得值对象。
allKeys: 返回所有键集合。
下面看NSDictionary数组的示例代码:
- import Foundation
- let keyString : NSString = "one two three four five" ①
- var keys : NSArray = keyString.componentsSeparatedByString(" ") ②
- let valuestring : NSString = "alpha bravo charlie delta echo" ③
- var values : NSArray = valuestring.componentsSeparatedByString(" ") ④
- vardict : NSDictionary = NSDictionary(objects: keys, forKeys:values) ⑤
- NSLog("%@", dict.description) ⑥
- varvalue:NSString = dict.objectForKey("three") as NSString ⑦
- NSLog("three = %@", value)
- varkys = dict.allKeys ⑧
- for item : AnyObject in kys { ⑨
- var key = item as NSString
- NSLog("%@ - %@", key, dict.objectForKey(key) as NSString)
- }
上述代码第①行和第③行代码是NSString字符串,字符串由单词和空格组成。第②行和第④行代码使用空格分割字符串,返回类型是NSArray数组。
第⑤行代码实例化NSDictionary对象,objects参数是值数组values,forKeys参数是。第⑥行代码description属性是获得字典的内容。第⑦行代码是通过objectForKey方法读取键对应的值,并且转换为NSString类型。
第⑧行代码dict.allKeys是获得所有的键集合kys,第⑨行代码是遍历键集合kys。
输出结果如下:
- 2014-07-06 20:19:07.274 PlaygroundStub_OSX[4110:303] {
- five = echo;
- four = delta;
- one = alpha;
- three = charlie;
- two = bravo;
- }
- 2014-07-06 20:19:07.281 PlaygroundStub_OSX[4110:303] three = charlie
- 2014-07-06 20:19:07.296 PlaygroundStub_OSX[4110:303] one - alpha
- 2014-07-06 20:19:07.300 PlaygroundStub_OSX[4110:303] five - echo
- 2014-07-06 20:19:07.305 PlaygroundStub_OSX[4110:303] three - charlie
- 2014-07-06 20:19:07.308 PlaygroundStub_OSX[4110:303] two - bravo
- 2014-07-06 20:19:07.313 PlaygroundStub_OSX[4110:303] four - delta
二、NSMutableDictionary类
NSMutableDictionary是NSDictionary的子类,它有很多方法和属性,下面总结其常用的方法和属性。
-setObject:forKey: 通过键和值。
-removeObjectForKey: 按照键移除值。
下面看NSDictionary数组的示例代码:
- import Foundation
- var mutable : NSMutableDictionary = NSMutableDictionary() ①
- // add objects
- mutable.setObject("Tom", forKey: "[email protected]") ②
- mutable.setObject("Bob", forKey: "[email protected]")
- NSLog("%@", mutable.description)
- var keys = mutable.allKeys
- for item : AnyObject in keys {
- var key = item as NSString
- NSLog("%@ - %@", key, mutable.objectForKey(key) as NSString)
- }
上述代码第①行是实例化NSMutableDictionary,第②行代码是通过setObject方法添加键和值。
输出结果如下:
- 2014-07-06 20:42:11.596 PlaygroundStub_OSX[4332:303] {
- "[email protected]" = Bob;
- "[email protected]" = Tom;
- }
- 2014-07-06 20:42:11.605 PlaygroundStub_OSX[4332:303] [email protected] - Bob
- 2014-07-06 20:42:11.608 PlaygroundStub_OSX[4332:303] [email protected] – Tom
三、NSDictionary与Dictionary之间的关系
NSDictionary与Dictionary之间的关系如同NSArray与Array之间的关系,Swift在底层能够将它们自动地桥接起来,一个NSDictionary对象桥接之后的结果是[NSObject : AnyObject]字典(值为NSObject 类型,键为AnyObject 类型的Dictionary字典)。
下面我们看一个使用Dictionary和NSDictionary的示例:
- import Foundation ①
- let keyString : NSString = "one two three four five"
- let keys : NSArray = keyString.componentsSeparatedByString(" ")
- let valueString : NSString = "alpha bravo charlie delta echo"
- let values : NSArray = valueString.componentsSeparatedByString(" ")
- let foundationDict : NSDictionary = NSDictionary(objects:values, forKeys:keys) ②
- let swiftDict : Dictionary = foundationDict ③
- println(swiftDict.description)
- let value: AnyObject? = swiftDict["three"] ④
- println("three = \(value)")
- for (key, value) in swiftDict { ⑤
- println ("\(key) - \(value)")
- }
代码第①行是引入Foundation。第②行代码声明并初始化NSDictionary字典,第③行代码是将NSDictionary字典赋值给Dictionary字典,这个过程也发生了类型转换,不仅是NSDictionary到Dictionary的转换,而且它们的内部元素也发生了转换。
第④行代码是从Dictionary字典取three键对应的值,它的类型是可选的AnyObject类型,这是因为有可能取不到这个值。第⑤行代码是遍历Dictionary字典键和值集合。
欢迎关注智捷iOS课堂微信公共平台