1
votes

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.

1
Is there are reason you are creating a new autoreleased string from searchText rather than just using searchText itself?dreamlax
I was wondering about that too, but in my test it made no difference. Maybe he just simplified it from a more elaborate formatting operation leading to the search term...VoidPointer
@dreamlax, Thats a remnant from the actual code I use because I format the searchText before searching in the code. Its the same as using searchText itself as you said.The Fat Oracle

1 Answers

2
votes

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...