NSMutalbleDictionary
2016-08-23 本文已影响25人
哔哩哔哩智能喵
//创建一个空的可变字典
NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//给可变字典赋值
[dicM setObject:@"lxc"forKey:@"name"];
//取出键值对添加到dicM中
[dicM setValuesForKeysWithDictionary:@{@"age":@"23",
@"height":@"1.78"}];
//获取字典中key对应的值
NSString *name = dicM[@"name"];
//删除字典中的key和对应的值
[dicM removeObjectForKey:@"height"];
//修改字典中key对应的值
[dicM setObject:@"18" forKey:@"age"];
//如果在不可变字典出现了同名的key,那么后面的key对应的值不会保存
NSDictionary *dic = @{@"name":@"cyf",@"name":@"xxx"};
//如果在可变字典出现了同名的key,那么后面的key会覆盖前面的
NSMutableDictionary *dM = [NSMutableDictionary dictionaryWithObjects:@[@"lxc",@"cyf"] forKeys:@[@"name",@"name"]];
//NSArray是有序的,NSDictionary是无序的
//NSArray是通过脚标的方式访问元素,NSDictionary是通过key访问元