@implementation NSArray (Find) 到底是什么以及它给出的警告?

发布于 2024-09-29 02:18:53 字数 935 浏览 1 评论 0原文

关于以下代码的几个问题:

@implementation NSArray (Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value {
   NSMutableArray *matches = [NSMutableArray array];
   for (id object in self) {
     id objectValue = [object valueForKeyPath:keyPath];
     if ([objectValue isEqual:value] || objectValue == value) [matches addObject:object];         
   }
   return matches;
}

1- (Find) 的作用是什么?我在做这些实现时也看到过类似的词,那么它到底在做什么呢?这是一个关键字,还是只是让我知道?

2-我从这里得到了代码: http://probableinteractive.com/2009/2/ 13/keypaths.html 但是当我将它放在我的项目中并调用它时,

NSArray *filterResults = [allResults findAllWhereKeyPath:@"firstname" equals:firstname];

它会返回警告 'NSArray' 可能不会响应 '-findAllWhereKeyPath:equals:' 并且如果我运行它,它崩溃了。 我已将代码放在 .m 的开头、.h 处,并将其更改为 NSMutableArray,但我不断收到警告。那么,我应该如何解决这个问题呢?

A couple of questions regarding the following code:

@implementation NSArray (Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value {
   NSMutableArray *matches = [NSMutableArray array];
   for (id object in self) {
     id objectValue = [object valueForKeyPath:keyPath];
     if ([objectValue isEqual:value] || objectValue == value) [matches addObject:object];         
   }
   return matches;
}

1- What does (Find) do? I've seen other words like this when doing these implementations, so what exactly is it doing? Is it a keyword, or just for me to know?

2- I got the code from here: http://probablyinteractive.com/2009/2/13/keypaths.html But when I place it on my project and call it

NSArray *filterResults = [allResults findAllWhereKeyPath:@"firstname" equals:firstname];

it returns the warning 'NSArray' may not respond to '-findAllWhereKeyPath:equals:' and if I run it, it crashes.
I've placed the code at the beginning of the .m, at the .h and changed it to NSMutableArray, but I keep getting the warning. So, how should I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

日裸衫吸 2024-10-06 02:18:53
  1. 此方法返回所有包含值对象的 keyPath。

  2. 要使此类别发挥作用,您应该执行以下操作:
    创建 NSArray(Find).h 和 NSArray(Find).m 文件:

NSArray(Find).h:

#import <Foundation/Foundation.h>

@interface NSArray(Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value;
@end

NSArray(Find).m:

@implementation NSArray (Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value {
   NSMutableArray *matches = [NSMutableArray array];
   for (id object in self) {
     id objectValue = [object valueForKeyPath:keyPath];
     if ([objectValue isEqual:value] || objectValue == value) [matches addObject:object];         
   }
   return matches;
}

这两个文件都应添加到您的项目中。将 NSArray(Find).h 导入到 .m 文件,您要在其中使用类别:

#import "NSArray(Find).h"

findAllWhereKeyPath:equals: 应该可以工作。

  1. This method returns all keyPaths, that contains value object.

  2. To make this category work you should do the following:
    Create NSArray(Find).h and NSArray(Find).m files:

NSArray(Find).h:

#import <Foundation/Foundation.h>

@interface NSArray(Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value;
@end

NSArray(Find).m:

@implementation NSArray (Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value {
   NSMutableArray *matches = [NSMutableArray array];
   for (id object in self) {
     id objectValue = [object valueForKeyPath:keyPath];
     if ([objectValue isEqual:value] || objectValue == value) [matches addObject:object];         
   }
   return matches;
}

Both files should be added to your project. Import NSArray(Find).h to the .m file, where you want to use your category:

#import "NSArray(Find).h"

findAllWhereKeyPath:equals: should work then.

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