Why doesn't this NSPredicate work?
I have a very simple NSPredicate as such:
NSPredicate *sPre开发者_Python百科dicate = [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
精彩评论