iOS NSDictionary 字典

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。