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 |
|