搜索结果始终为 0(NSArray 和 NSPredicate)

发布于 2024-09-30 07:35:07 字数 707 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

暮凉 2024-10-07 07:35:07

你的代码是正确的。我尝试过:

@interface Foo : NSObject

@property (nonatomic, retain) NSString * name;

@end
@implementation Foo

@synthesize name;

@end

NSMutableArray * a = [NSMutableArray array];
for (int i = 0; i < 100; ++i) {
    Foo * f = [[Foo alloc] init];
    [f setName:[NSString stringWithFormat:@"%d", i]];
    [a addObject:f];
    [f release];
}

NSPredicate * p = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", @"1"];
NSArray * filtered = [a filteredArrayUsingPredicate:p];
NSLog(@"%@", [filtered valueForKey:@"name"]);

日志:

2010-10-29 10:51:22.103 EmptyFoundation[49934:a0f] (
    1,
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19
)

这让我问:你的originalArray是空的吗?

Your code is correct. I tried it:

@interface Foo : NSObject

@property (nonatomic, retain) NSString * name;

@end
@implementation Foo

@synthesize name;

@end

NSMutableArray * a = [NSMutableArray array];
for (int i = 0; i < 100; ++i) {
    Foo * f = [[Foo alloc] init];
    [f setName:[NSString stringWithFormat:@"%d", i]];
    [a addObject:f];
    [f release];
}

NSPredicate * p = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", @"1"];
NSArray * filtered = [a filteredArrayUsingPredicate:p];
NSLog(@"%@", [filtered valueForKey:@"name"]);

Logs:

2010-10-29 10:51:22.103 EmptyFoundation[49934:a0f] (
    1,
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19
)

Which leads me to ask: Is your originalArray empty?

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