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]%@',現在它完美地工作! –