views:

44

answers:

2

I have an array of objects (Users) each user has an nsset named "devices" Is it possible to filter so the array returns all users who have a device with a specific name.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"devices.category==%@", @"mobile"];
myArray = [allUsersArray filteredArrayUsingPredicate:predicate];
A: 

http://stackoverflow.com/questions/110332/filtering-nsarray-into-a-new-nsarray-in-objective-c

That should help

Richard J. Ross III
no that doesn't help :( that's a simple predicate which i have already got to work. My case is completely different
aryaxt
+2  A: 

You've almost got it, just a little bit off.

Each User has a set of Devices. This means that when you invoke [aUser valueForKeyPath:@"devices.category"], it's going to give you a collection of the aggregation of the devices' categories.

In other words, if your user has 2 devices, and they (respectively) have a category of "mobile" and "desktop", then "devices.category" will return (mobile, desktop). This is a vector value. It contains multiple elements.

However, you're comparing this to a scalar value (a single element), @"mobile".

What I think you're going for is wanting to select all users that have at least one device that's in the "mobile" category, correct? If that's the case, then you just need to use the ANY keyword, and make your predicate thusly:

[NSPredicate predicateWithFormat:@"ANY devices.category = %@", @"mobile"]

For more information on these aggregate functions, check out the Predicate Programming Guide.

Dave DeLong
thanks a lot, exactly what i wanted
aryaxt