@implementation NSArray (Find) 到底是什么以及它给出的警告?
关于以下代码的几个问题:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此方法返回所有包含值对象的 keyPath。
要使此类别发挥作用,您应该执行以下操作:
创建 NSArray(Find).h 和 NSArray(Find).m 文件:
NSArray(Find).h:
NSArray(Find).m:
这两个文件都应添加到您的项目中。将 NSArray(Find).h 导入到 .m 文件,您要在其中使用类别:
findAllWhereKeyPath:equals:
应该可以工作。This method returns all keyPaths, that contains value object.
To make this category work you should do the following:
Create NSArray(Find).h and NSArray(Find).m files:
NSArray(Find).h:
NSArray(Find).m:
Both files should be added to your project. Import NSArray(Find).h to the .m file, where you want to use your category:
findAllWhereKeyPath:equals:
should work then.