如何执行 NSArray FilteredArrayUsingPredicate,其中谓词是方法?
如何执行 NSArray FilteredArrayUsingPredicate,其中谓词是方法?这就是一个简单的代码示例吗?
我一直在尝试检查谓词 doco 并感到有点困惑。我可以看到它是如何进行简单检查的,但是如果我有一个检查需要几行 Objective-C 代码来实现有效的代码:
- 使用filteredArrayUsingPredicate 谓词过滤 NSArray
- 将是一种以某种方式采用的方法输入变量,执行任何检查和平衡,然后返回 TRUE/FALSE 作为返回值,以确定该项目是否应该被过滤,
谢谢
How can I perform an NSArray filteredArrayUsingPredicate where the predicate is a method? That is what would a simple code example look like here?
I've been trying to go through the predicate doco and getting a little confused. I can see how it works for simple checks, but if I have a check which requires a few lines of objective-c code to implement what would the code look like to effectively:
- filter an NSArray using filteredArrayUsingPredicate
- predicate would be a method which somehow takes an input variable(s), performs whatever checks and balances and then gives a TRUE/FALSE back as a return value re whether the item should be filtered or not
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要您使用 iOS 4.0 及更高版本,您就会很高兴知道这非常简单(以下内容在 3.x 上不可用)。
您可以使用
predicateWithBlock
方法创建一个 NSPredicate,该 NSPredicate 采用返回 YES 或 NO 的块作为其参数。所以几乎正是您想要的(如果您不熟悉块,它们基本上是封装方法的一种方式。请参见此处:http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1)+ (NSPredicate *)predicateWithBlock:( BOOL (^)(id评估对象,NSDictionary *绑定))块
As long as you're going with iOS 4.0 and above you'll be glad to know this is really straightforward (the following isn't available on 3.x).
You can use the
predicateWithBlock
method to create a NSPredicate that takes a block that returns YES or NO as its argument. So pretty much exactly what you want (if you're not familiar with blocks they're basically a way to encapsulate a method. See here: http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1)+ (NSPredicate *)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block
您可以使用 @lxt 建议的
predicateWithBlock:
方法,也可以使用FUNCTION
方法。这将使您构建一个如下所示的谓词:如果您使用该谓词来过滤数组,则:
SELF
将迭代地成为数组中的每个项目 数组中-mySuperMethod:
调用的方法-mySuperMethod:
将接收aParameter
作为方法的参数-mySuperMethod:
将返回 < code>BOOL 已装箱在NSNumber
<<这一点非常重要,-mySuperMethod:
返回YES
的对象都将包含在过滤数组中。有关此语法的更多信息,请查看这篇博文。
那么为什么您想使用这种方法而不是块方法呢?我可以想到两个原因:
aParameter
符合
协议就可以。块无法序列化。然而,如果这些都不是要求,那么从长远来看,块方法可能会更好,因为它更明显和可读。 :)
You can use the
predicateWithBlock:
approach that @lxt suggested, or you can use theFUNCTION
approach. This would have you build a predicate that looks like this:If you use that predicate to filter an array, then:
SELF
will iteratively be each item in the array-mySuperMethod:
method invoked-mySuperMethod:
will receiveaParameter
as the parameter to the method-mySuperMethod:
will return aBOOL
that has been boxed in anNSNumber
<< this is very importantYES
from-mySuperMethod:
will be included in the filtered array.For more information on this syntax, check out this blog post.
So why might you want to use this approach over the block approach? I can think of two reasons:
aParameter
conforms to the<NSCoding>
protocol. Blocks cannot be serialized.However, if neither of those are requirements, then the block approach will probably be better in the long run, since it's more obvious and readable. :)