nullLululi nullLululi - 3 months ago 13
Objective-C Question

NSPredicate Regex error

I'm dealing with a document management page. I used

NSPredicate
regular expression while got the
searchBar.text
from user inputs. An error occurred (as the image below) while the inputs are brackets in Chinese form but went well in English form. If anyone has come across this matter? Thank you.

enter image description here

Here is my code,crash happen to the last row.

NSString * searchStr = [NSString stringWithFormat:@"fileName like[cd] \'*%@*\'",searchDisplayController.searchBar.text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:searchStr];
self.fileArrayAfterPre = [[NSMutableArray alloc] initWithArray:[fileArr filteredArrayUsingPredicate:predicate]];

Answer

LIKE: It is use in wildcard expression.

CONTAIN: According to your question you should use the contain[cd] because it match the file with search text.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fileName CONTAINS[cd] %@",searchDisplayController.searchBar.text];

    self.fileArrayAfterPre =  [[NSMutableArray alloc] initWithArray:[fileArr filteredArrayUsingPredicate:predicate]];