NSDictionary创建、引用以及遍历等功能(Objective-C 开发范例)

来源:互联网 发布:网络电视机顶合 编辑:程序博客网 时间:2024/06/05 11:57

NSDictionary创建、引用、遍历及操纵字典内容等功能

(1)创建字典

        应用需要对列表中的对象进行分组,你想要通过键来引用对象。
解决方案
       可以通过两个Objective-C Foundation 类——NSDictionary 与NSMutableDictionary 来创建带有键的对象列表。如果不需要修改列表,就使用NSDictionary;如果在后面要向字典中添加或删除对象,就使用NSMutableDictionary。
说明
       在Objective-C 中,字典的创建与其他对象一样:可以使用alloc 与init 构造函数,或者使用dictionaryWithObjects:forKeys:等便捷函数创建字典。如果使用NSDictionary 创建字典,那么字典一旦创建完毕后,就无法再进行修改。使用NSMutableDictionary 创建的字典可以在后面进行修改。在下面的示例中,创建的字典包含了不同语言版本的Hello World。该短语的每个版本都会被录入到相应的语言中:

NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World",                         @"Bonjour tout le monde", @"Hola Mundo", nil];NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french",                      @"spanish", nil];NSDictionary *dictionary2 = [NSDictionarydictionaryWithObjects:listOfObjects forKeys:listOfKeys];

       NSDictionary 构造函数arrayWithObjects:forKeys:需要两个数组作为参数。第一个数组必须包含待存储的对象,第二个数组必须包含与这些对象关联的键。如果使用NSMutableDictionary,那么可以通过相同的构造函数创建字典(NSMutable-Dictionary 是NSDictionary 的子类)。如果后面还要向字典中添加对象,那么还可以通过alloc 与init 来创建NSMutableDictionary 对象。表3-3 完整列出了NSDictionary 与NSMutableDictionary 类的构造函数,程序清单列出了相关代码。


代码
       程序清单main.m

#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){     @autoreleasepool {       NSDictionary *dictionary1 = [[NSDictionary alloc] init];       NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World",                                @"Bonjour tout le monde", @"Hola Mundo", nil];       NSArray *listOfKeys = [NSArray arrayWithObjects:@"english",                             @"french", @"spanish", nil];       NSDictionary *dictionary2 = [NSDictionary       dictionaryWithObjects:listOfObject forKeys:listOfKeys];       NSLog(@"dictionary2 = %@", dictionary2);     }     return 0;}
使用
      要想使用上述代码,请从Xcode 构建并运行Mac 应用。可以设置断点并使用Xcode调试器查看这些字典的内容。下一点将会介绍如何引用每个字典元素,这样就可以将它们的内容打印到日志中或是在程序的其他地方使用它们了。你会看到字典的全部内容被打印到日志中:
dictionary2 = {      english = "Hello World";      french = "Bonjour tout le monde";      spanish = "Hola Mundo";}

(2)引用数组中的对象

解决方案
       使用objectForKey:方法可以获得与你提供的键相对应的对象引用。
说明
       NSDictionary 根据你提供的键来组织对象。这样一来,查找对象就变得非常简单和快速。只需要使用objectForKey:并提供与想要查找的对象相对应的键,就可以获得对象引用:

NSString *helloWorld = [dictionary objectForKey:@"english"];
示例代码
#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){      @autoreleasepool {      NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World",                               @"Bonjour tout le monde", @"Hola Mundo", nil];      NSArray *listOfKeys = [NSArray arrayWithObjects:@"english",                            @"french", @"spanish", nil];      NSDictionary *dictionary = [NSDictionary      dictionaryWithObjects:listOfObjects forKeys:listOfKeys];      NSString *helloWorld = [dictionary objectForKey:@"english"];      NSLog(@"%@", helloWorld);     }    return 0;}

(3)获取字典中元素的数量

解决方案
       NSDictionary 对象提供了count 属性,可以通过该属性获得字典中元素的数量。
说明
       要想使用count 属性,可以对字典对象使用点符号(dictionary.count)或是发送count 消息([dictionary count]),从而获得字典中元素的数量

示例代码

#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){      @autoreleasepool {         NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World",                                  @"Bonjour tout le monde", @"Hola Mundo", nil];         NSArray *listOfKeys = [NSArray arrayWithObjects:@"english",                               @"french", @"spanish", nil];         NSDictionary *dictionary = [NSDictionary         dictionaryWithObjects:listOfObjects forKeys:listOfKeys];         NSString *helloWorld = [dictionary objectForKey:@"english"];         NSLog(@"%@", helloWorld);    }    return 0;}

(3) 遍历字典

解决方案
       使用NSDictionary 函数allValues 将字典转换为数组,接下来就可以使用for-each 循环了,此外还可以使用enumerateKeysAndObjectsUsingBlock:方法来处理字典中的每个对象。
说明
       NSDictionary 对象提供了内置方式来遍历对象列表。然而,如果想要使用之前一篇文章数组遍历节中介绍的方式,那么可以临时将字典的键与对象内容转换为数组。例如,要想使用for-each 循环遍历字典中的对象,可以通过如下代码实现:

for (NSString *s in [dictionary allValues]) {     NSLog(@"value: %@", s);}
       NSDictionary 函数allValues 会返回以数组而非字典形式组织的对象。函数allKeys 会将键值作为数组返回:

for (NSString *s in [dictionary allKeys]) {     NSLog(@"key: %@", s);}
      还可以使用块,通过enumerateKeysAndObjectsUsingBlock:方法针对字典中的每个对象执行代码。可以用来定义代码块,然后应用到字典中的每个对象,同时又不必创建for-each循环或是获得数组版本的字典引用:
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {NSLog(@"key = %@ and obj = %@", key, obj);}];
示例代码

#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){     @autoreleasepool {       NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World",                                @"Bonjour tout le monde", @"Hola Mundo", nil];       NSArray *listOfKeys = [NSArray arrayWithObjects:@"english",                             @"french", @"spanish", nil];       NSDictionary *dictionary = [NSDictionary                                  dictionaryWithObjects:listOfObjects forKeys:listOfKeys];       for (NSString *s in [dictionary allValues]) {          NSLog(@"value: %@", s);       }       for (NSString *s in [dictionary allKeys]) {          NSLog(@"key: %@", s);       }       [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj,BOOL *stop) {         NSLog(@"key = %@ and obj = %@", key, obj);       }];     }   return 0;}
使用
      要想使用上述代码,请从Xcode 构建并运行Mac 应用。日志消息会显示出使用各种方式遍历字典后的结果:
value: Hello Worldvalue: Bonjour tout le mondevalue: Hola Mundokey: englishkey: frenchkey: spanishkey = english and obj = Hello Worldkey = french and obj = Bonjour tout le mondekey = spanish and obj = Hola Mundo



阅读全文
0 0