问题
I am saving different codes in a NSMutableDictionary and checking later if a specific code is in the dict.
No I want to remove the entry which contain the code.
Here you see how my array with the dict looks:
2013-04-28 12:43:23.877 myApp[9422:907] pushArray: (
{
code = 123;
titel = "Test 01";
},
{
code = 456;
titel = "Test 02";
},
{
code = 789;
titel = "Test 03";
}
)
Here you see how I search for the code:
for(NSMutableDictionary * pushDict in pushArray) {
NSString * codeFromDict = [pushDict objectForKey: @"code"];
if([code_I_search isEqualToString: codeFromDict]){
// This is called if the code is found
}
}
This is all working fine, but now I don't now how to remove the entry...
The result should be:
If I search for code 789
and say "remove" this would be removed:
{
code = 789;
titel = "Test 03";
}
I tried [pushDict removeAllObjects];
but thats not working.
回答1:
You want to remove that whole entry, right?
If so, then make your "pushArray
" a NSMutableArray
and then you can remove the offending dictionary entries via:
for(NSDictionary * pushDict in pushArray) {
NSString * codeFromDict = [pushDict objectForKey: @"code"];
if([code_I_search isEqualToString: codeFromDict]){
[pushArray removeObject: pushDict];
}
}
The way you're doing it up there, you're removing all objects from the pushDict, but not the actual pushDict object from the (immutable) array.
来源:https://stackoverflow.com/questions/16263487/remove-specific-objects-from-nsmutabledictionary