ios之NSDictionary、NSMutableDictionary使用小结【转】

来自:http://www.xuebuyuan.com/2178612.html

NSDictionary和NSMutableDictionary属于字典类,类似于java中的map,里面存储的是key-value键值对。

NSDictionary是不可改变的字典,也就是说一旦初始化后,里面的键值对只有查询的份了。

如果要对键值对进行“增删查改”的话,就得用NSMutableDictionary。

详细操作介绍如下:

//创建字典
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
NSLog(@"dic1 :%@", dic1);


//创建多个字典
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
					  @"value1", @"key1",
					  @"value2", @"key2",
					  @"value3", @"key3",
					  @"value4", @"key4",
					  nil];
NSLog(@"dic2 :%@", dic2);


//根据现有的字典创建字典
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
NSLog(@"dic3 :%@", dic3);


//根据key获取value
NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);

//获取字典数量
NSLog(@"dic count :%d", dic3.count);

//所有的键集合
NSArray *keys = [dic3 allKeys];
NSLog(@"keys :%@", keys);

//所有值集合
NSArray *values = [dic3 allValues];
NSLog(@"values :%@", values);



NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
								   @"mvalue1", @"mkey1",
								   @"mvalue2", @"mkey2", nil];
//添加现有的字典数据
[mutableDic addEntriesFromDictionary:dic3];
NSLog(@"mutableDic :%@",mutableDic);

//添加新的键值对象
[mutableDic setValue:@"set1" forKey:@"setKey1"];
NSLog(@"set value for key :%@",mutableDic);

//以新的字典数据覆盖旧的字典数据
[mutableDic setDictionary:dic2];
NSLog(@" set dictionary :%@",mutableDic);

//根据key删除value
[mutableDic removeObjectForKey:@"key1"];
NSLog(@"removeForkey :%@",mutableDic);

//快速遍历
for(id key in mutableDic) {
	NSLog(@"key :%@  value :%@", key, [mutableDic objectForKey:key]);
}

//枚举遍历
NSEnumerator *enumerator = [mutableDic keyEnumerator];
id key = [enumerator nextObject];
while (key) {
	NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);
	key = [enumerator nextObject];
}


//根据key数组删除元素
[mutableDic removeObjectsForKeys:keys];
NSLog(@"removeObjectsForKeys :%@",mutableDic);

[mutableDic removeAllObjects];
//删除所有元素
NSLog(@"remove all :%@", mutableDic);

结果:

2016-11-24 14:46:25.725 dictionaryTest[1905:72894] Hello, World!
2016-11-24 14:46:25.731 dictionaryTest[1905:72894] dic1 :{
    key = value;
}
2016-11-24 14:46:25.732 dictionaryTest[1905:72894] dic2 :{
    key1 = value1;
    key2 = value2;
    key3 = value3;
    key4 = value4;
}
2016-11-24 14:46:25.733 dictionaryTest[1905:72894] dic3 :{
    key1 = value1;
    key2 = value2;
    key3 = value3;
    key4 = value4;
}
2016-11-24 14:46:25.734 dictionaryTest[1905:72894] key3 value :value3
2016-11-24 14:46:25.734 dictionaryTest[1905:72894] dic count :4
2016-11-24 14:46:25.735 dictionaryTest[1905:72894] keys :(
    key3,
    key1,
    key4,
    key2
)
2016-11-24 14:46:25.735 dictionaryTest[1905:72894] values :(
    value3,
    value1,
    value4,
    value2
)
2016-11-24 14:46:25.735 dictionaryTest[1905:72894] mutableDic :{
    key1 = value1;
    key2 = value2;
    key3 = value3;
    key4 = value4;
    mkey1 = mvalue1;
    mkey2 = mvalue2;
}
2016-11-24 14:46:25.735 dictionaryTest[1905:72894] set value for key :{
    key1 = value1;
    key2 = value2;
    key3 = value3;
    key4 = value4;
    mkey1 = mvalue1;
    mkey2 = mvalue2;
    setKey1 = set1;
}
2016-11-24 14:46:25.736 dictionaryTest[1905:72894]  set dictionary :{
    key1 = value1;
    key2 = value2;
    key3 = value3;
    key4 = value4;
}
2016-11-24 14:46:25.888 dictionaryTest[1905:72894] removeForkey :{
    key2 = value2;
    key3 = value3;
    key4 = value4;
}
2016-11-24 14:46:25.889 dictionaryTest[1905:72894] key :key2  value :value2
2016-11-24 14:46:25.889 dictionaryTest[1905:72894] key :key3  value :value3
2016-11-24 14:46:25.889 dictionaryTest[1905:72894] key :key4  value :value4
2016-11-24 14:46:25.889 dictionaryTest[1905:72894] enumerator :value2
2016-11-24 14:46:25.889 dictionaryTest[1905:72894] enumerator :value3
2016-11-24 14:46:25.889 dictionaryTest[1905:72894] enumerator :value4
2016-11-24 14:46:25.889 dictionaryTest[1905:72894] removeObjectsForKeys :{
}
2016-11-24 14:46:25.891 dictionaryTest[1905:72894] remove all :{
}
Program ended with exit code: 0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值