Nie*_*aan 2 macos cocoa core-data objective-c 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
Run Code Online (Sandbox Code Playgroud)
当我在projectName为“ 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.'
Run Code Online (Sandbox Code Playgroud)
po请求向我提供了以下信息:
po request
<NSFetchRequest: 0x101348ee0> (entity: Project; predicate: (name LIKE[c] "asdf\\"); sortDescriptors: ((null)); type: NSManagedObjectResultType; )
Run Code Online (Sandbox Code Playgroud)
在我看来这是一个简单的问题,但无法在Internet上找到解决方案,谁知道呢?
LIKE在谓词句柄中*并?作为通配符,其中?匹配1个字符并*匹配0个或多个字符。这些通配符可以被转义为\*或\?逐字匹配星号或问号。
结果,必须像\\在“ LIKE”查询中一样对反斜杠本身进行转义以匹配单个反斜杠。一个单反斜线在搜索模式的结束将导致错误(这是你的情况发生了什么)。
如果您不需要通配符操作,您可以使用==,BEGINSWITH或CONTAINS代替。所有这些都可以与结合使用[c]。
| 归档时间: |
|
| 查看次数: |
1435 次 |
| 最近记录: |