搜索结果始终为 0(NSArray 和 NSPredicate)
我有一个 NSArray
,其中包含像这样定义的 Foo
对象:
@interface Foo : NSObject <NSCopying> {
NSString *name;
}
@property (nonatomic, retain) NSString *name;
@end
和查询:
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", filterString];
//NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", filterString];
filteredArray = [[originalArray filteredArrayUsingPredicate:filterPredicate] mutableCopy];
但这不起作用。我总是得到 0 结果。
所以,我的问题是:
我是否应该始终使用 NSPredicate 来仅搜索具有特定键的 NSDictionary 对象,或者只要有属性就可以使用它来搜索任何对象/与查询匹配的方法(在本例中:名称)?
提前致谢
I have an NSArray
which holds Foo
objects defined like so:
@interface Foo : NSObject <NSCopying> {
NSString *name;
}
@property (nonatomic, retain) NSString *name;
@end
and the query:
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", filterString];
//NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", filterString];
filteredArray = [[originalArray filteredArrayUsingPredicate:filterPredicate] mutableCopy];
and this is not working. I always get 0 results.
So, my question is:
Should I always use NSPredicate
for searching only NSDictionary
objects with a certain key or can I use it for searching any object as long as there is a property/method that matches the query (in this case: name)?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码是正确的。我尝试过:
日志:
这让我问:你的
originalArray
是空的吗?Your code is correct. I tried it:
Logs:
Which leads me to ask: Is your
originalArray
empty?