开发者

Objective-C: NSPredicate LIKE doesn't like spaces. Is there an escape sequence to replace blanks with?

开发者 https://www.devze.com 2022-12-19 09:29 出处:网络
I have a NSPredicate that l开发者_JS百科ooks like this: NSPredicate *likePredicate3= [NSPredicate predicateWithFormat:@\"synonyms LIKE[cd] %@\",[NSString stringWithFormat:@\"%@\", searchText]];

I have a NSPredicate that l开发者_JS百科ooks 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.


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

0

精彩评论

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