J. Moss
J. Moss

Reputation: 33

How do I remove objects from an NSMutableArray matching an NSNumber

I have an array like this:

    {
    id = 8281;
    name = “John”;
    title = “Title One“;
     },
    {
    id = 8729;
    name = “Bob”;
    title = “Title Two“;
    },
    {
    id = 8499;
    name = “Dave”;
    title = “Title Three“;
    }

I want to remove the array containing a specific ID.

As an example, let's say I have:

NSNumber *removeThis =  '8281' ; 

And my array above is named "stories".

I have tried:

[stories removeObject:removeThis];

but that does not work.

I have also tried:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id != %@", removeThis];
NSArray *results = [stories filteredArrayUsingPredicate:predicate];

But the data is never removed from the array.

Can someone point me in the right direction?

Upvotes: 0

Views: 204

Answers (1)

zpasternack
zpasternack

Reputation: 17898

I don't think this:

NSNumber *removeThis =  '8281'; 

...is even valid Objective-C. Use @(number) to make an NSNumber literal, like:

NSNumber *removeThis =  @(8281); 

From there, it should work exactly as you typed it:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id != %@", removeThis];
NSArray *results = [stories filteredArrayUsingPredicate:predicate];

Upvotes: 2

Related Questions