Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2623133
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-08-20 17:39:29

NSDictionary使用小结
http://blog.csdn.net/ms2146/article/details/8656787
  1. #import <Foundation/Foundation.h>  
  2.   
  3.   
  4. int main(int argc, const char * argv[])  
  5. {  
  6.   
  7.     @autoreleasepool {  
  8.   
  9.         //创建字典  
  10.         NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
  11.         NSLog(@"dic1 :%@", dic1);  
  12.           
  13.           
  14.         //创建多个字典  
  15.         NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:  
  16.                               @"value1", @"key1",  
  17.                               @"value2", @"key2",  
  18.                               @"value3", @"key3",  
  19.                               @"value4", @"key4",  
  20.                               nil];  
  21.         NSLog(@"dic2 :%@", dic2);  
  22.           
  23.           
  24.         //根据现有的字典创建字典  
  25.         NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];  
  26.         NSLog(@"dic3 :%@", dic3);  
  27.           
  28.           
  29.         //根据key获取value  
  30.         NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);  
  31.           
  32.         //获取字典数量  
  33.         NSLog(@"dic count :%d", dic3.count);  
  34.           
  35.         //所有的键集合  
  36.         NSArray *keys = [dic3 allKeys];  
  37.         NSLog(@"keys :%@", keys);  
  38.           
  39.         //所有值集合  
  40.         NSArray *values = [dic3 allValues];  
  41.         NSLog(@"values :%@", values);  
  42.           
  43.           
  44.           
  45.         NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  46.                                            @"mvalue1", @"mkey1",  
  47.                                            @"mvalue2", @"mkey2", nil];  
  48.         //添加现有的字典数据  
  49.         [mutableDic addEntriesFromDictionary:dic3];  
  50.         NSLog(@"mutableDic :%@",mutableDic);  
  51.           
  52.         //添加新的键值对象  
  53.         [mutableDic setValue:@"set1" forKey:@"setKey1"];  
  54.         NSLog(@"set value for key :%@",mutableDic);  
  55.           
  56.         //以新的字典数据覆盖旧的字典数据  
  57.         [mutableDic setDictionary:dic2];  
  58.         NSLog(@" set dictionary :%@",mutableDic);  
  59.           
  60.         //根据key删除value  
  61.         [mutableDic removeObjectForKey:@"key1"];  
  62.         NSLog(@"removeForkey :%@",mutableDic);  
  63.           
  64.         //快速遍历  
  65.         for(id key in mutableDic) {  
  66.             NSLog(@"key :%@  value :%@", key, [mutableDic objectForKey:key]);  
  67.         }  
  68.           
  69.         //枚举遍历  
  70.         NSEnumerator *enumerator = [mutableDic keyEnumerator];  
  71.         id key = [enumerator nextObject];  
  72.         while (key) {  
  73.             NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);  
  74.             key = [enumerator nextObject];  
  75.         }  
  76.           
  77.           
  78.         //根据key数组删除元素  
  79.         [mutableDic removeObjectsForKeys:keys];  
  80.         NSLog(@"removeObjectsForKeys :%@",mutableDic);  
  81.           
  82.         [mutableDic removeAllObjects];  
  83.         //删除所有元素  
  84.         NSLog(@"remove all :%@", mutableDic);  
  85.     }  
  86.     return 0;  
  87. }  


日志:


[html] view plaincopy
  1.  dic1 :{  
  2.     key = value;  
  3. }  
  4.  dic2 :{  
  5.     key1 = value1;  
  6.     key2 = value2;  
  7.     key3 = value3;  
  8.     key4 = value4;  
  9. }  
  10.  dic3 :{  
  11.     key1 = value1;  
  12.     key2 = value2;  
  13.     key3 = value3;  
  14.     key4 = value4;  
  15. }  
  16.  key3 value :value3  
  17.  dic count :4  
  18.  keys :(  
  19.     key2,  
  20.     key3,  
  21.     key1,  
  22.     key4  
  23. )  
  24.  values :(  
  25.     value2,  
  26.     value3,  
  27.     value1,  
  28.     value4  
  29. )  
  30.  mutableDic :{  
  31.     key1 = value1;  
  32.     key2 = value2;  
  33.     key3 = value3;  
  34.     key4 = value4;  
  35.     mkey1 = mvalue1;  
  36.     mkey2 = mvalue2;  
  37. }  
  38.  set value for key :{  
  39.     key1 = value1;  
  40.     key2 = value2;  
  41.     key3 = value3;  
  42.     key4 = value4;  
  43.     mkey1 = mvalue1;  
  44.     mkey2 = mvalue2;  
  45.     setKey1 = set1;  
  46. }  
  47.   set dictionary :{  
  48.     key1 = value1;  
  49.     key2 = value2;  
  50.     key3 = value3;  
  51.     key4 = value4;  
  52. }  
  53.  removeForkey :{  
  54.     key2 = value2;  
  55.     key3 = value3;  
  56.     key4 = value4;  
  57. }  
  58.  key :key4  value :value4  
  59.  key :key2  value :value2  
  60.  key :key3  value :value3  
  61.  enumerator :value4  
  62.  enumerator :value2  
  63.  enumerator :value3  
  64.  removeObjectsForKeys :{  
  65. }  
  66.  remove all :{  
  67. }  
阅读(305) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~