从NSMutableDictionary中删除特定对象 [英] Remove specific objects from NSMutableDictionary
问题描述
我将不同的代码保存在NSMutableDictionary中,然后稍后检查字典中是否包含特定代码。
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.
在这里,您将看到带有dict的数组的样子:
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";
}
)
在这里您看到我如何搜索代码:
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...
结果应为:
如果我搜索代码 789
并说删除,则将其删除:
If I search for code 789
and say "remove" this would be removed:
{
code = 789;
titel = "Test 03";
}
我尝试了 [pushDict removeAllObjects];
但那不起作用。
I tried [pushDict removeAllObjects];
but thats not working.
推荐答案
您要删除整个条目,对吗?
You want to remove that whole entry, right?
如果是这样,则将您的 pushArray
设置为 NSMutableArray
,然后就可以通过以下方式删除有问题的字典条目:
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];
}
}
您在那里做事的方式正在从pushDict中删除所有对象,而不是从(不可变)数组中删除实际的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.
这篇关于从NSMutableDictionary中删除特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!