views:

105

answers:

1

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?

+3  A: 

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

bobDevil
Damn I thought I had tried that already. But yes, that was it. Thanks!
Jasconius