两个 NSNumber 数组的 NSPredicate [英] NSPredicate for two NSNumber arrays

查看:70
本文介绍了两个 NSNumber 数组的 NSPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我的搜索功能编写谓词时遇到了一些困难,我认为你会帮忙的.所以基本上我有两个 NSNumbers 数组.我希望我的谓词满足以下条件:

I have a bit of a hard time writing a predicate for my search functionality and thought you'd be bale to help. So basically I have two arrays of NSNumbers. I want my predicate to satisfy the following:

If a number's integerValue in array A matches any integerValue in array B.

我不想为此解决方案使用任何类型的循环.这是我目前所拥有的

I don't want to use any sort of loop for this solution. Here's what I have so far

ANY integerValue == ANY //how do I pass the entire array here and ask for the integerValue of each member? 

推荐答案

ANY 操作员会处理这个问题.

The ANY operator will handle that.

因为从你的问题中很难说哪个数组是自我",所以用正常的谓词说法,我会在没有 self 的情况下编写它:

Since it is a bit difficult to say from your question which of the arrays is "self" in normal predicate parlance, I'll write it without a self:

NSArray *arrayA = @[@2, @3, @7];
NSArray *arrayB = @[@2, @4, @9];

NSPredicate *pred = [NSPredicate predicateWithFormat: @"ANY %@ IN %@", arrayA, arrayB];

由于缺少self",它必须使用 nil 作为对象进行评估,但这工作正常:

Due to the lack of a "self", it will have to be evaluated with nil as the object, but that works fine:

BOOL matched = [pred evaluateWithObject: nil];

如果你更喜欢自己"在谓词中,你可以直接输入:

If you prefer to have a "self" in the predicate, you can just enter it:

NSPredicate *pred = [NSPredicate predicateWithFormat: @"ANY self IN %@", arrayB];
BOOL matched = [pred evaluateWithObject: arrayA];

结果是一样的.

如果两个数组中都包含任何整数,则上述谓词的计算结果为真,这就是我阅读您的问题的方式.

The predicate above evaluates to true if any integer is included in both arrays, which is how I read your question.

这意味着,从概念上讲,您似乎在测试两组数字是否相交.NSSet 的方法 intersectsSet: 对此进行检查,因此另一种进行测试的方法是将您的数字保留为集合并测试交集:

This means that, conceptually speaking, you seem to be testing whether two sets of numbers intersect each other. NSSet's method intersectsSet: checks that, so another way to do the test would be to keep your numbers as sets and test for intersection:

matched = [setA intersectsSet: setB];

这篇关于两个 NSNumber 数组的 NSPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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