views:

489

answers:

1

I have a NSPredicate that looks like this:

NSPredicate *likePredicate3= [NSPredicate predicateWithFormat:@"synonyms LIKE[cd] %@",[NSString stringWithFormat:@"%@", searchText]];

I apply it to an NSArray of objects of a class that has the 'synonyms' property.

It works fine when the searchText is a whole word such as "Thanks". However if I try to use strings with space in them such as 'Thank you', it fails and the predicate search does not find the match in the array.

Is there a way to ask NSPredicate to work with words that have a blank space(s) in them?

thanks.

+2  A: 

In principle, what you are doing should work. I ran this example:

NSString *search = @"a b";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", 
                          [NSString stringWithFormat:@"%@", search]];
NSArray *array = [[NSArray arrayWithObjects:@"a b", @"ä B", @"ccc", nil]
                  filteredArrayUsingPredicate: predicate];
NSLog(@"result: %@", array);

Output is:

Running…
2010-02-04 20:05:41.770 predicate2[74163:a0f] result: (
    "a b",
    "\U00e4 b"
)

Maybe your searchString isn't what you think it is...

VoidPointer
@VoidPointer, yes that was my guess too after a while and I have been trying to figure out what that could be. I am searching an array loaded from a plist and it works for some cases, but not for others.
The Fat Oracle
In the documentation, some of the examples are using quotes around the %@ placeholder in the argument to predicateWithFormat:. In my tests, that led to the placeholder not being evaluated at all. Maybe you could try that: [NSPredicate predicateWithFormat:@"SELF LIKE[cd] '%@'", ...]
VoidPointer