NSPredicate match any characters

The LIKE string comparison in predicates allows for wildcards * and ?, where * matches 0 or more characters. Therefore, if you transform your search text into

NSString *searchWithWildcards = @"*T*e*s*t* *S*t*r*A*p*p*l*";

by inserting a * at the beginning, between all characters, and at the end of the original search text by using something like this

NSMutableString *searchWithWildcards = [NSMutableString stringWithFormat:@"*%@*", self.searchField.text];
if (searchWithWildcards.length > 3)
    for (int i = 2; i < self.searchField.text.length * 2; i += 2)
        [searchWithWildcards insertString:@"*" atIndex:i];

then you can use

[NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", searchWithWildcards];

The predicate searches for the characters in the given order, with arbitrary other characters between them.


The transformation can for example be done with the following code:

NSMutableString *searchWithWildcards = [@"*" mutableCopy];
[searchText enumerateSubstringsInRange:NSMakeRange(0, [searchText length])
                   options:NSStringEnumerationByComposedCharacterSequences
                usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                    [searchWithWildcards appendString:substring];
                    [searchWithWildcards appendString:@"*"];
                }];