NSMutableDictionary 示例

+ dictionaryWithCapacity:

建立並返回一個可變字典,最初為其提供足夠的分配記憶體以容納給定數量的條目。

NSMutableDictionary *dict =  [NSMutableDictionary dictionaryWithCapacity:1];
NSLog(@"%@",dict);

- 在裡面

初始化新分配的可變字典。

NSMutableDictionary *dict =  [[NSMutableDictionary alloc] init];        
NSLog(@"%@",dict);

+ dictionaryWithSharedKeySet:

建立一個可變字典,該字典針對處理一組已知金鑰進行了優化。

id sharedKeySet = [NSDictionary sharedKeySetForKeys:@[@"key1", @"key2"]]; // returns NSSharedKeySet
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithSharedKeySet:sharedKeySet];
dict[@"key1"] = @"Easy";
dict[@"key2"] = @"Tutorial";
//We can an object thats not in the shared keyset
dict[@"key3"] = @"Website";
NSLog(@"%@",dict);  

OUTPUT

{
    key1 = Eezy;
    key2 = Tutorials;
    key3 = Website;
}

將條目新增到可變字典中

- setObject:forKey:

將給定的鍵值對新增到字典中。

NSMutableDictionary *dict =  [NSMutableDictionary dictionary];
[dict setObject:@"Easy" forKey:@"Key1"];
NSLog(@"%@",dict);

OUTPUT

{
    Key1 = Eezy;
}

- setObject:forKeyedSubscript:

將給定的鍵值對新增到字典中。

NSMutableDictionary *dict =  [NSMutableDictionary dictionary];
[dict setObject:@"Easy" forKeyedSubscript:@"Key1"];
NSLog(@"%@",dict);  

輸出{Key1 =簡單; }