I'm using Core Data and I have a predicate like this in a fetch request:
NSString *predicateStr = [NSString stringWithFormat:@"name like[c] '%@'",name];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateStr];
This works great with one exception: sometimes it's possible that the name may end with a backslash ( "\" ) since that part is user-generated and can also come from outside the app.
When it does, I get an error like this:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Unable to parse the format string "(name like[c] 'mqcu\')"'
So I tried using stringByReplacingOccurrencesOfString like this:
NSString *predicateStr = [NSString stringWithFormat:@"(name like[c] '%@')", [name stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]];
but then I get an error like this:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'The backslash is a wildcard char, and cannot appear unescaped at the end of a string.'
How do I properly escape the backslash?
%@in quotes in the predicate string cannot have worked at all. - Martin RstringWithFormatto build the predicate ... ? - Martin R