Reputation: 8947
I have an IPhone application in which I have an NSMutableArray
of dictionaries. When selecting each row of the table, I am deleting the dictionaries by using
if (indexPath.row <[self.newmessagearrays count])
{
[messagearrays removeObjectAtIndex:indexPath.row];
}
I have a scenario in which I want to remove all the dictionaries in which my "from id" key is the same, when the row is tapped. I tried like this, but with no effect:
NSPredicate * pr = [NSPredicate predicateWithFormat:@"userid == %@",fromidstring];
[messagearrays filterUsingPredicate:pr];
Upvotes: 1
Views: 232
Reputation: 950
Array keeps objects at some index, dictionary keeps object and keys so your algorithm should be like this
Get object(dictionary) from array one by one using loop, check that your dictionary has that specific key.
if yes, remove that object from array using function removeObjectAtIndex
Upvotes: 0
Reputation: 104698
You can filter the array in place using NSPredicate
:
NSPredicate * pr = [NSPredicate predicateWithFormat:@"containsKey == SOME_KEY"];
[messageArrays filterUsingPredicate:pr];
Update
In response to edit/clarification:
NSPredicate * pr = [NSPredicate predicateWithBlock:
^(id dictionary, NSDictionary * bindings) {
return (BOOL)![[dictionary objectForKey:@"fromid"] isEqual:@"190"];
}];
[messageArrays filterUsingPredicate:pr];
Upvotes: 4
Reputation: 1014
NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
NSDictionary *item;
NSUInteger index = 0;
for (item in yourArrayOfNSDictionaries) {
if ([dictionary objectForKey:theKeyYouWantToCheckFor] != nil) [discardedItems addIndex:index];
index++;
}
[originalArrayOfItems removeObjectsAtIndexes:discardedItems];
Upvotes: 0