Iphone 如何在字典中插入具有值的新键

Iphone 如何在字典中插入具有值的新键,iphone,key,nsmutabledictionary,Iphone,Key,Nsmutabledictionary,我想在现有字典中插入具有相应值的键 我可以为字典中的现有键设置值,但无法添加新的键值 任何帮助都将不胜感激。措辞是不变的。请改用NSMuteableDictionary 对象:setObject:forKey: 测试是否存在密钥: [[aDict allKeys] containsObject:@"key"]; 使用NSMutableDictionary NSMutableDictionary *yourMutableDictionary = [NSMutableDictionary allo

我想在现有字典中插入具有相应值的键

我可以为字典中的现有键设置值,但无法添加新的键值


任何帮助都将不胜感激。

措辞是不变的。请改用NSMuteableDictionary

对象:
setObject:forKey:

测试是否存在密钥:

[[aDict allKeys] containsObject:@"key"];

使用NSMutableDictionary

NSMutableDictionary *yourMutableDictionary = [NSMutableDictionary alloc] init];
[yourMutableDictionary setObject:@"Value" forKey:@"your key"];
[dict setObject:@"Value" forKey:@"Key"];
Swift更新:

以下是上述代码的准确swift副本

var yourMutableDictionary = NSMutableDictionary()
yourMutableDictionary.setObject("Value", forKey: "Key")
但我建议你还是用斯威夫特字典的方式

var yourMutableDictionary = [String: AnyObject]() //Open close bracket represents initialization

//The reason for AnyObject is a dictionary's value can be String or
//Array or Dictionary so it is generically written as AnyObject

yourMutableDictionary["Key"] = "Value"
通过使用此方法,我们可以将新值添加到NSMutableDictionary

NSMutableDictionary *yourMutableDictionary = [NSMutableDictionary alloc] init];
[yourMutableDictionary setObject:@"Value" forKey:@"your key"];
[dict setObject:@"Value" forKey:@"Key"];
要知道字典里是否有钥匙

[[dict allKeys] containsObject:@"key"];

嗨,我以字典的格式从json中获取内容,并从中将内容添加到其他字典中

  //here contract is my json dictionary 
  NSArray *projectDBList =[contract allKeys];//listing all the keys in dict 

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

  [projectDBList enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL *stop) {

  [projectsList setObject:[contract objectForKey:obj] forKey:obj];

  }];

对于nsmutable dictionary,如何插入新的键值如何知道给定的键是否存在于dictionary[yourMutableDictionary allKeys]中;这将返回该字典中所有键的数组,并将其与给定的键进行比较,顺便说一下,如果您必须转换回NSDictionary,您可以将其转换为NSDictionary,如将其转换为NSDictionary,以及如何将任何对象类添加为value和int作为key?cv::Mat作为value和int作为key如何将其添加到NSMutableDictionary?