我必须从核心数据中搜索包含方括号的路径。 但是当我尝试创建如下所示的搜索查询时,xcode会崩溃。
path == /my/local/[path]
我试图逃避方括号,从一个括号到16个括号,但没有任何作用。
该路径来自其他应用程序,因此无法更改。 我该怎么办? 感谢您的帮助!
编辑:
我制作NSPredicate
的代码是这样的。
NSPredicate *preTest = [NSPredicate predicateWithFormat:@"localFilePath == /[test]/"];
更新:
我发现了一些奇怪的东西
NSPredicate *testPre = [NSPredicate predicateWithFormat:@"localFilePath == %@", uploadFile.remotePath];
NSPredicate *what = [NSPredicate predicateWithFormat:@"localFilePath == /"];
uploadFile.remotePath等于@" /"。
第一个是好的,但第二个崩溃了!
我认为这似乎与我的问题有关。任何想法?
感谢您在我的问题上留出时间! :)
答案 0 :(得分:0)
如果您需要匹配[
和]
,则可以使用matches
运算符,该运算符使用regular expressions。例如:
NSArray *array = @[@"[apple]", @"[boy]", @"[dog]", @"cat"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"self matches %@", @"\\[dog\\]"];
NSLog(@"%@", [array filteredArrayUsingPredicate:pred]);
关于您的更新:predicateWithFormat
与stringWithFormat
不同,因为它会执行一些有助于构建谓词的其他作业。例如
[NSPredicate predicateWithFormat@"localFilePath == %@", @"/"]
相当于
[NSPredicate predicateWithFormat@"localFilePath == \"/\""]
您可以看到引号自动添加。此外,predicateWithFormat
会接受一些特殊的替换,例如%K
(对于动态属性)。
Apple提供了一个很好的tutorial关于使用您可能想要查看的谓词。