1
我有以下代码:NSPredicate转义反斜杠字符
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like[c] %@", projectName];
[fetchRequest setPredicate:predicate];
[moc executeFetchRequest:request error:error]; // moc & error are defined elsewhere
当我与项目名是运行这段代码“ASDF”一切都很正常。然而,当此值被设置为“ASDF \”(注意在端反斜杠),下面的异常被呈现:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The backslash is a wildcard char, and cannot appear unescaped at the end of a string.'
PO请求为我提供了以下信息:
po request
<NSFetchRequest: 0x101348ee0> (entity: Project; predicate: (name LIKE[c] "asdf\\"); sortDescriptors: ((null)); type: NSManagedObjectResultType;)
对我而言,这似乎是一个简单的问题,但无法在互联网上找到解决方案,谁知道呢?
但是“asdf /”在最后有斜线,而不是反斜线。 –
你真的需要“LIKE”吗?和*通配符?否则,您可以使用“CONTAINS”。 –
谢谢马丁!它不必处理?和*通配符,所以我将谓词改为'name == [c]%@',现在它完美地工作! –