黑马程序员技术交流社区
标题:
NSMutableDictionary使用简介
[打印本页]
作者:
dzb5461883
时间:
2015-8-17 08:00
标题:
NSMutableDictionary使用简介
1. 创建NSMutableDictionary
NSMutableDictionary *mDict = [NSMutableDictionary dictionary]; //创建一个空字典
NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithCapacity:3];//创建一个容量为3的字典
2. 给字典添加键值对
[mDict setValue:@"value1" forKey:@"key1"];
[mDict setValue:@"value2" forKey:@"key2"];
注意:键值(key)不可以重复
3. 快速创建NSMutableDictionary
NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
mDict.dictionary = @{@"key1":@"value1",@"key2":@"value2"};
3. 删除字典内容
[mDict removeObjectForKey:@"key1"]; //删除键值为key1的键值对
[mDict removeAllObjects]; //删除所有键值对
4. 修改value
[mDict setObject:@"value3" forKey:@"key1"]; //注意区分setValue和setObject
mDict[@"key1"] = @"value4";
5. 遍历dictionary
NSArray *array = [mDict allKeys]; //获取所有键值
//根据所有键值来遍历dictionary
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2