0
votes

I'm working on a project where I'm downloading a whole mess of JSON and then making a pretty UITableView out of it.

I've got the JSON parsed out into a nice NSMutableArray which I then feed into the UITableView. Everything works and looks pretty.

The problem that I'm having is that my NSMutableArray has a dictionary object in it that I'm wanting to use to perform filtering of the tableview on.

This is a NSLog of an object in the NSMutableArray that I'm looking at:

{
id = 123456;
    issues =     (
                {
            id = 4;
            name = "Ape";
        },
                {
            id = 25;
            name = "Chimp";
        },
                {
            id = 28;
            name = "Human";
        }
    );
status = open;
}

What I want to write is a predicate filter on any of the name column in the issues dictionary in the NSMutableArray.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"name", @"Human"];
NSArray *predicateFilteredArray = [tableViewArray filteredArrayUsingPredicate:predicate]; 

However I can't figure out the right format for the predicate variable that will match names in issues. I've read and reread the Predicate Programming guide and about half of the stack overflow items flagged NSPredicate and I can't figure out the syntax. I've tried lots of variations of SELF and am just plain old stuck.

2
Did my answer help? Please let me know if you need more information. - Martin R

2 Answers

2
votes

If I understand the problem correctly, the following predicate should work:

[NSPredicate predicateWithFormat:@"ANY issues.name == %@", @"Human"]

This gives all objects from the array where at least one of the "issues" has a "name" which is equal to "Human".

0
votes

I've written a library of category methods, called Linq to ObjectiveC, which makes this sort of thing a little easier. From what I can gather, you have an array of items, where each item has a property called issues which is itself an array?

If you want to find all the items that have an issue named 'Human', you can perform the following:

NSArray* matches = [inputArray where:^BOOL(id item) {
    return [[item issues] any:^BOOL(id issue) {
        return [[issue name] equalTo:@"Human"];
    }];
}]

This uses the Linq to ObjectiveC where and any methods.