iPhone和放大器;目标C - 过滤使用NS predicate一个数组? [英] iphone & Objective C - Filtering an array using NSPredicate?

查看:139
本文介绍了iPhone和放大器;目标C - 过滤使用NS predicate一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象(用户)的数组
每个用户都有一个命名的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆