0

I have an array of objects that I need to filter. I know that I should b using NSPredicate but can't seem to find an answer to filtering by a string in an object, inside of another object.

Here is what a single object of the array looks like:

{
    "data_center"
    {
        "id":1,
        "site_id":17,
        "name":"Building",
        "shortname":"ORL-Building",
        "created_at":"2011-10-28T06:32:39Z",
        "updated_at":"2011-10-28T06:32:39Z",
        "site":
        {
            "id":17,
            "name":"Orlando",
            "shortname":"ORL"
        }
    }
}

How can I filter the Array using Site.name in the nested object? The array itself is an NSArray which requires filteredArrayUsingPredicate:.

Also, The way that I am finding the data to filter the array by is by pulling it from a UIPickerView, So I have to retrieve the object that was used from a different pickerview, and filter a second array based on the string for site name.

I couldn't seem to find a good answer to this one, If anyone has any advice I would be much appreciative.

0

1 Answer 1

3

Predicates can use keypaths to navigate into the data structure, so you could do something like:

@"data_center.site.name LIKE %@", name

You could also look at using enumerateObjectsUsingBlock: to build the list of items that you are interested in.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, what I posted was the original JSON response. The array is dataCentersArray with a data center object, that has a nested site object. But this does give me an idea.

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.