Objective-C语言NSMutableDictionary 示例
例子
+字典与容量:
创建并返回一个可变字典,最初为其分配足够的内存以保存给定数量的条目。
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1]; NSLog(@"%@",dict);
-在里面
初始化一个新分配的可变字典。
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSLog(@"%@",dict);
+dictionaryWithSharedKeySet:
创建一个可变字典,该字典针对处理一组已知的键进行了优化。
id sharedKeySet = [NSDictionary sharedKeySetForKeys:@[@"key1", @"key2"]]; //返回NSSharedKeySet NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithSharedKeySet:sharedKeySet]; dict[@"key1"] = @"Easy"; dict[@"key2"] = @"Tutorial"; //我们可以使用不在共享密钥集中的对象 dict[@"key3"] = @"Website"; NSLog(@"%@",dict);
输出
{ key1 = Eezy; key2 = Tutorials; key3 = Website; }
将条目添加到可变字典
-setObject:forKey:
将给定的键值对添加到字典中。
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"Easy" forKey:@"Key1"]; NSLog(@"%@",dict);
输出
{ Key1 = Eezy; }
-setObject:forKeyedSubscript:
将给定的键值对添加到字典中。
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"Easy" forKeyedSubscript:@"Key1"]; NSLog(@"%@",dict);
输出{Key1=Easy;}