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;}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章