NSDictionary不可变字典
字典中的数据都是无序的;
初始化
NSDictionary *D1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",nil];
//按一个值,一个键的顺序排列;
NSDictionary *D2 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsi",@"name",@"30",@"age",nil];
//便利构造器;
NSDictionary *D3 = [[NSDictionary alloc] initWithObjects:@[@"wang",@"20"] forKeys:@[@"name",@"age"]];
//键值一一对应;
NSDictionary *D4 = [NSDictionary dictionaryWithOjects:@[@"zhao",@"12"] forKeys:@[@"name",@"age"]];
//相应的便利构造器;
字面量
NSDictionary *D5 = @{@"name":@"zhou",@"age":@"20"};
//键在值前,用':'连接,不同键值对间用','连接;
获取所有Key
NSLog(@"%@",[D5 allKeys]);//allKeys 方法返回值是一个数组
获取所有值
NSLog(@"%@",[D5 allValues]);//allValues 方法返回值是一个数组
NSMutableDictionary可变字典
初始化
NSMutableDictionary *D6 = [@{@"name":@"laowang",@"age":@77,@"sex":@"m",@"height":@175} mutableCopy];
//先设置字面量,在通过mutableCopay方法生成可变字典;
设置键值对
[D6 setObject:@"zhaosi" forKey:@"name"];
//如果有键就修改,如果没这个键就添加;
[D6 addEntriesFromDictionary:D5];
//将D5中的键值对复制到D6中,如果D6中已经存在某键,则D5的键值对将会覆盖D6原有的;
[D6 setDictionary:D5];
//将D6变成D5;
删除键值对
[D6 removeObjectForKey:@"name"];
[D6 removeObjectsForKeys@[@"sex",@"age"];
//删除一组Key对应的键值对;
[D6 removeAllObjects];
//删除所有键值对;