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 =简单; }