开发者

No Result when searching through an NSArray of NSDictionaries using NSPredicate

开发者 https://www.devze.com 2023-01-26 11:27 出处:网络
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 NSPred

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 filteredArrayUsingPr开发者_如何学JAVAedicate: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!


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.


You can use "like" instead:

first like[c] *%@*


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消