简体   繁体   中英

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?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM