开发者

Why doesn't this NSPredicate work?

开发者 https://www.devze.com 2023-01-04 20:04 出处:网络
I have a very simple NSPredicate as such: NSPredicate *sPre开发者_Python百科dicate = [NSPredicate predicateWithFormat:@\"name beginswith \'%@\'\", theString];

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

0

精彩评论

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

关注公众号