I have a very simple NSPredicate as such:
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith '%@'", theString];
[matchingTags filterUsingPredicate:sPredicate];
This causes the array to have 0 results when theString == "p"
However, when I do this:
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith 'p'"];
[matchingTags filterUsingPredicate:sPredicate];
I get over 100 results, as expected.
I have checked "theString" with NSLog() and its value is correct. I feel like I am missing some crucial secret. Possibly because I am using a string and not a character?
Thoughts?
Check out the documentation here
If you use variable substitution using %@ (such as firstName like %@), the quotation marks are added for you automatically.
So basically, it's looking for names that start with "p", instead of p. Changing your code to:
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith %@", theString];
should work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.