Remove specific objects from NSMutableDictionary

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:55:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!