天天看点

Object-c之可变字典

//创建可变字典

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one", @"1", @"two",@"2",@"three",@"3", nil];

NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];

NSMutableDictionary *dict3 = @{@"1":@"one",@"2":@"two",@"3":@"three"};

//添加键值对(如果这个键存在,那就是修改该值)

[dict setObject:@"four" forKey:@"4"];

NSLog(@"dict = %@", dict);

//输出 : dict = {1 = one;2 = two;3 = three;4 = four;}

//删除键值对

[dict removeObjectForKey:@"4"];

NSLog(@"dict = %@", dict);

//输出 : dict = {1 = one;2 = two;3 = three;}

//删除多个键值对

[dict removeObjectsForKeys:@[@"1",@"2"]];

NSLog(@"dict = %@",dict);

//输出 : dict = {3 = three;}

//删除所有键值对

[dict removeAllObjects];

NSLog(@"dict = %@", dict);

//把字典dict3中的键值对添加到dict中

[dict setDictionary:dict3];

NSLog(@"dict = %@", dict);

// 输出 : dict = {1 = one;2 =two;3 = three;}

继续阅读