iPhone和放大器;目标C - 过滤使用NS predicate一个数组? [英] iphone & Objective C - Filtering an array using NSPredicate?
问题描述
我有对象(用户)的数组
每个用户都有一个命名的NSSet设备
是否有可能进行过滤,从而数组返回谁拥有具有特定名称的设备的所有用户。
NS predicate * predicate = NS predicate predicateWithFormat:@devices.category ==%@,@移动] ;
myArray的= [allUsersArray filteredArrayUsing predicate:predicate]。
您已经几乎得到了它,只是一点点了。
每个用户
有一组设备
的。这意味着,当你调用 [aUser valueForKeyPath:@devices.category]
,这将会给你的集的的的聚集设备的类别。
在换句话说,如果你的用户有2台设备,他们(分别)有一个类别
移动和桌面,那么devices.category
将返回(移动,桌面)
。这是一个矢量值。它包含多个元素。
不过,你比较这一个标值(单个元素), @移动
。
我想你会的是希望选择至少有一个设备是在移动的范畴,正确的所有用户?如果是这样的话,那么你只需要使用任何
关键词,让你的predicate正是如此:
[NS predicate predicateWithFormat:@ANY devices.category =%@,@移动]
有关这些聚合函数的更多信息,请查看<一个href=\"http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/$p$pdicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-215891\">$p$pdicate编程指南。
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];
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.
这篇关于iPhone和放大器;目标C - 过滤使用NS predicate一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!