Q
如何插入詞典
22
A
回答
41
使用的NSMutableDictionary
NSMutableDictionary *yourMutableDictionary = [NSMutableDictionary alloc] init];
[yourMutableDictionary setObject:@"Value" forKey:@"your key"];
更新斯威夫特:
以下是上述
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"
1
NSDictionnary是不可變的。改爲使用NSMuteableDictiory。
adding對象:setObject:forKey:
編輯
測試,如果關鍵是存在:
[[aDict allKeys] containsObject:@"key"];
+0
爲nsmutable字典如何插入新的鍵值 – 2010-11-26 14:04:38
1
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
通過使用這種方法,我們可以將新值添加到的NSMutableDictionary
[dict setObject:@"Value" forKey:@"Key"];
要知道wheather關鍵在字典中
[[dict allKeys] containsObject:@"key"];
0
你好我收到內容來自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];
}];
如何知道定key是否存在或不字典 – 2010-11-26 15:17:45