在我的项目中,我在一个实体中有一组属性。其中一个是作为字符串的路径。我想要所有将我的字符串作为路径中的子路径的记录。
例子: 路径:/var/mobile/Applications/C4ECC129-36D2-4428-8BB0-6B1F4C971FC1/Library/Caches/Packages/1000
Mystring : 库/缓存/包/1000
我尝试使用 Like 和 contains 如下但失败了。
[NSPredicate predicateWithFormat:@"bookPath like[c] '%%%@'",Mystring];
[NSPredicate predicateWithFormat:@"bookPath Contains[cd] '%@'",Mystring];
谁能帮忙写一个谓词来获取那些包含 mystring 的记录。
真的帮了我很多。
提前发送
最佳答案
你需要有这样的谓词
[fecthRequest setPredicate:[NSPredicate predicateWithFormat:@"bookPath endswith[cd] %@", myString]];
或者这个
[fecthRequest setPredicate:[NSPredicate predicateWithFormat:@"bookPath contains[cd] %@", myString]];
没有结果是由于您的 %@
被单引号引起的。来自文档(Dynamic Property Names):
string variables are surrounded by quotation marks when they are substituted into a format string using %@
关于谓词我真的建议使用第一个,如果你正在寻找的子路径总是在原始路径的最后部分。
关于使用谓词,我真的建议阅读String Comparisons .
希望对您有所帮助。
关于ios - 用于从 coredata 查询字符串的子字符串的谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12974737/