添加、删除,修改值
NSMutableDictionary *propertyDict;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:value forKey:key];//add
[propertyDict removeObjectForKey:key] //remove
[propertyDict setValue:value forKey:key] //setvalue
2020/8/5凌晨00:00补充
可以当做映射使用:把字符串转换成另外对对应的字符串。
字典可以初始化为:
NSDictionary *errorDict;
//初始化
errorDict = @{“nwqwq”:@"12",
“asada”:@"22",
“aadd”:@"as13",
“sas”:@"12ee",
"aas":@"1233",
"asa":@"adc23"
};
//查找
str = [errorDict objectForKey:str];
还可以用map实现,和这个道理差不多,
以后会补充
2020、8、5
使用dictionaryWithObjectsAndKeys 初始化 NSMutableDictionary
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:value1, @"key1", value2, @"key2", value3, @"key3", nil];
dict 中有3个key-value对,原因是 dictionaryWithObjectsAndKeys 遇到 object 为 nil 时结束。
和下面也一样的写法
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
[dict setObject:value1 forKey:@"key1"];
[dict setObject:value2 forKey:@"key2"];
[dict setObject:value3 forKey:@"key3"];