0

I have an NSMutableArray (self.allPeople) containing a number of dictionaries (people). That said, I also have an NSMutableArray (self.participants) that contains a series of numbers:

self.participants

Here they are: (
    179,
    125,
    231
)

I want filter self.allPeople, and only return dictionaries in which the key "nid" is equal to one of the numbers contained in self.participants.

How can I filter self.allPeople (valueForKey:@"nid") with each number in self.participants?

This works if I only want to filter by a single string value, but I need it to filter for all numbers:

  NSPredicate *p = [NSPredicate predicateWithFormat:@"nid CONTAINS[cd] %@", @"179"];
  self.results = [self.allPeople filteredArrayUsingPredicate:p];

1 Answer 1

1

Just use IN for the array, like this:

NSArray<NSDictionary<NSString *, NSNumber *> *> *allPeople = @[
    @{ @"nid": @120 },
    @{ @"nid": @121 },
    @{ @"nid": @122 },
    @{ @"nid": @123 },
];
NSArray<NSNumber *> *participants = @[ @120, @123 ];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nid IN %@", participants];
NSArray<NSDictionary<NSString *, NSNumber *> *> *results = [allPeople filteredArrayUsingPredicate:predicate];
NSLog(@"RESULTS: %@", results);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.