使用 NSPredicate 搜索 NSDictionaries 的 NSArray 时没有结果

发布于 2024-10-04 04:56:40 字数 868 浏览 7 评论 0原文

嘿, 我有一个 NSDictionaries 数组(例如 [tempArray addObject:[[[NSMutableDictionary alloc] initWithObjects:someArray forKeys:keys] autorelease]]; ),当我尝试使用 NSPredicate 搜索它时,它不断给出我“没有结果”。 (TempArray 变为 self.patentList)

我没有编译时警告或错误,如果我 NSLog([self.filteredArray count]); 那么它也会返回 0。

这是我的相关代码:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(first contains[cd] %@)", self.searchBar.text];
self.filteredArray = [self.patientList filteredArrayUsingPredicate:predicate];
return [self.filteredArray count];

self. PatientList 和 self.filteredArray 分别是 NSMutableArray 和 NSArray 类型(将 self.filteredArray 更改为 NSMutableArray 没有帮助)。

我也尝试过使用 == 而不是 contains 。使用 @"SELF contains[cd] %@" 仅在键入完整项目时返回一个项目(即,如果键“name”是@“Bob”,那么如果我输入 Bob,它将显示它,但当我输入 Bo 或 ob 时则不会)。

我真的被这个难住了。

提前致谢!

Hey,
I have an Array of NSDictionaries (e.g. [tempArray addObject:[[[NSMutableDictionary alloc] initWithObjects:someArray forKeys:keys] autorelease]]; ) and when I try to search through it with NSPredicate it keeps giving me 'No Results'. (TempArray becomes self.patientList)

I have no compile-time warnings or errors, and if I NSLog([self.filteredArray count]); then it returns 0 as well.

Here is my relevant code:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(first contains[cd] %@)", self.searchBar.text];
self.filteredArray = [self.patientList filteredArrayUsingPredicate:predicate];
return [self.filteredArray count];

self.patientList and self.filteredArray are types NSMutableArray and NSArray respectively (changing self.filteredArray to NSMutableArray doesn't help).

I have tried using == instead of contains as well. Using @"SELF contains[cd] %@" only returns an item when the full item is typed (i.e. if the key 'name' is @"Bob" then if I type in Bob it will display it, but not when I type Bo or ob).

I'm really stumped on this one.

Thanks in advance!

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

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

发布评论

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

评论(3

芸娘子的小脾气 2024-10-11 04:56:40

显然,first 是使用谓词时的保留字。来自苹果的文档:

以下字词被保留:

“和”、“或”、“非”、“所有”、“任意”、“某些”、
无,例如,不区分大小写,CI,
匹配、包含、开始、
ENDSWITH、BETWEEN、NULL、NIL、SELF、
对、是、错、否、第一个、最后一个、
大小、ANYKEY、子查询、演员表、
正确预测、错误预测

只需将您的密钥更改为其他内容,也许是名字,它就会起作用。

Apparently first is a reserved word when using predicates. From Apple's documentation:

The following words are reserved:

AND, OR, IN, NOT, ALL, ANY, SOME,
NONE, LIKE, CASEINSENSITIVE, CI,
MATCHES, CONTAINS, BEGINSWITH,
ENDSWITH, BETWEEN, NULL, NIL, SELF,
TRUE, YES, FALSE, NO, FIRST, LAST,
SIZE, ANYKEY, SUBQUERY, CAST,
TRUEPREDICATE, FALSEPREDICATE

Just change your key to something else, firstName perhaps, and it will work.

幸福还没到 2024-10-11 04:56:40

您可以使用“like”来代替:

first like[c] *%@*

You can use "like" instead:

first like[c] *%@*
风启觞 2024-10-11 04:56:40

乍一看,这似乎是正确的。那么:你的字典实际上有一个名为 first 的键吗?如果确实如此,情况是否完全匹配?

At first glance, it seems correct. So then: do your dictionaries actually have a key called first? And if they do, does it match the case exactly?

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