可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a array(dataArray) of NSDictionary "item". It has datas like "david"for key "name" and "85" for key "marks" etc for 5 students.I want to replace the mark of david to 90 with respect to the array index value(ie 0 for dictionary containing david and 85).How can i do it.Please help me.
EDIT:
The code for content in array is
[item setobject:name forkey:@"Name"]; [item setobject:mark forkey:@"Marks"]; [dataArray addOject:item]
The above code goes inside parsing, so i have array with 5 objetcs(students), their name and marks , now i want to replace the mark of the first object in the dataArray. Hope youcan understand the structure of my result.
回答1:
Here's what you can do:
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; NSDictionary *oldDict = (NSDictionary *)[dataArray objectAtIndex:0]; [newDict addEntriesFromDictionary:oldDict]; [newDict setObject:@"Don" forKey:@"Name"]; [dataArray replaceObjectAtIndex:0 withObject:newDict]; [newDict release];
Hope this helps!
回答2:
You first need an NSMutableDictionary with it you can change the key and value.
It would be like this:
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"david", @"name", "85", @"marks", nil]; [dict setObject:@"90" forKey:@"david"];
回答3:
If you want to update an object in a NSDictionary you should use NSMutableDictionary instead. NSMutableDictionary has the following EXTRA methods
Adding Entries
- setObject:forKey:
- setValue:forKey:
- addEntriesFromDictionary:
- setDictionary:
Removing Entries
- removeObjectForKey:
- removeAllObjects
- removeObjectsForKeys:
回答4:
// NSDictionary *dict = ... NSMutableDictionary *mutableDict = [dict mutableCopy]; [mutableDict setObject:@"myObject" forKey:@"myKey"]; dict = [mutableDict mutableCopy];
I hope it helps
回答5:
[davidsRecord setObject:@"100" forkey:@"mark"];