Reputation: 3285
I have a NSMutableArray
named _groupCategoryCurrentIds
whose data is in this format
(
{
id = 2;
name = Fashion;
},
{
id = 5;
name = Leisure;
},
{
id = 14;
name = Clothing;
},
{
id = 17;
name = Sports;
},
{
id = 2;
name = Fashion;
},
{
id = 36;
name = Men;
},
{
id = 34;
name = Woodland;
},
{
id = 30;
name = Accessories;
},
{
id = 4;
name = Entertainment;
},
{
id = 40;
name = Education;
}
)
I'm trying to delete the object with id = 40 and this is how I do it
_groupCategoryCurrentIds = [[_groupCategoryCurrentIds filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT id IN %@", @"40"]] mutableCopy];
But this deletes the object with id =4 as well. Any suggestion what I'm doing wrong?
Upvotes: 2
Views: 109
Reputation: 539685
Use
[NSPredicate predicateWithFormat:@"id != %@", @"40"]
or
[NSPredicate predicateWithFormat:@"id != %@", @40]
depending on whether the id is stored as a string or as a number.
Note that you can simplify your code to
[_groupCategoryCurrentIds filterUsingPredicate:...];
if _groupCategoryCurrentIds
is a mutable array.
"IN" in a predicate is used for testing membership in an array or set, for example
[NSPredicate predicateWithFormat:@"NOT id IN %@", @[@40, @50]]
to get all objects where the id is neither 40 nor 50.
Using "IN" with a string on the right-hand side is not documented in the Predicate Programming Guide. It seems to behave like "is a substring of", so that in your case
[NSPredicate predicateWithFormat:@"NOT id IN %@", @"40"]
gives all objects where the id is not a substring of "40". That would explain your result. But again, this is not documented behaviour.
Upvotes: 5