0

I have an array of NSDictionaries that I would like to filter. Each dictionary contains the key "tid" which corresponds to a string object. I would like to pull out all dictionaries for which their object for key "tid" is NOT equal to any of the string objects contained in a separate array.

For example, if I have an array called "stringObjects" and an array of dictionaries called "dictionaries", in pseudo code I want to do:

for (NSString *string in stringObjects) {
 //if a dictionary in dictionaries contains a string object for it's key @"tid" which is NOT equal to *string, then put it in an array
}

I've been trying to use NSPredicate to do this, but after several varied attempts, I can't get my desired result. Here is the code I most recently tried (which actually seems to create an array of every object for every dictionary in the array except objects for key @"tid"):

NSSet *savedThreadIds = //set of strings

NSMutableArray *filtered = [NSMutableArray array];

for (NSString *threadId in savedThreadIds) {
      [filtered addObjectsFromArray:[arrayOfDictionaries filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(%K != %@)", @"tid", threadId]]];     
}

Any help is appreciated.

1 Answer 1

0

If I understand your answer correctly, you're looking for NSCompoundPredicate:

NSMutableArray *predicates = [[NSMutableArray alloc] init];
for (NSString *string in stringObjects) {
    //Create an array of predicates
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"NOT (tid CONTAINS[cd] %@)",string];
    [predicates addObject:pred];
}
//Create a compound predicate from predicate array, with format P1 AND P2 AND P3 etc.
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
//Filter an your array using compoundPredicate.
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.