如何执行 NSArray FilteredArrayUsingPredicate,其中谓词是方法?

发布于 2024-10-24 12:50:31 字数 308 浏览 5 评论 0原文

如何执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-10-31 12:50:31

只要您使用 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

心房的律动 2024-10-31 12:50:31

您可以使用 @lxt 建议的 predicateWithBlock: 方法,也可以使用 FUNCTION 方法。这将使您构建一个如下所示的谓词:

[NSPredicate predicateWithFormat:@"FUNCTION(SELF, 'mySuperMethod:', %@)", aParameter];

如果您使用该谓词来过滤数组,则:

  • SELF 将迭代地成为数组中的每个项目 数组中
  • 的每个项目都将具有其 -mySuperMethod: 调用的方法
  • -mySuperMethod: 将接收 aParameter 作为方法的参数
  • -mySuperMethod: 将返回 < code>BOOL 已装箱在 NSNumber <<这一点非常重要,
  • 所有从 -mySuperMethod: 返回 YES 的对象都将包含在过滤数组中。

有关此语法的更多信息,请查看这篇博文

那么为什么您想使用这种方法而不是块方法呢?我可以想到两个原因:

  • 向后兼容性
    • 如果您需要在 Leopard 上使用此功能(Snow Leopard 中引入了 Mac 上的块)。
    • 如果您需要此功能在 iOS 4.0 之前的版本上运行(iOS 上的块是在 iOS 4.0 中引入的)。
  • 您想要序列化谓词以供存档和以后检索。只要 aParameter 符合 协议就可以。块无法序列化。

然而,如果这些都不是要求,那么从长远来看,块方法可能会更好,因为它更明显和可读。 :)

You can use the predicateWithBlock: approach that @lxt suggested, or you can use the FUNCTION approach. This would have you build a predicate that looks like this:

[NSPredicate predicateWithFormat:@"FUNCTION(SELF, 'mySuperMethod:', %@)", aParameter];

If you use that predicate to filter an array, then:

  • SELF will iteratively be each item in the array
  • each item in the array will have its -mySuperMethod: method invoked
  • -mySuperMethod: will receive aParameter as the parameter to the method
  • -mySuperMethod: will return a BOOL that has been boxed in an NSNumber << this is very important
  • all of the objects that return YES 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:

  • Backwards Compatibility
    • If you need this to work on Leopard (blocks on the Mac were introduced in Snow Leopard).
    • If you need this to work on iOS pre-4.0 (blocks on iOS were introduced in iOS 4.0).
  • You want to serialize the predicate for archival and later retrieval. This is fine as long as 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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文