ios - 用于从 coredata 查询字符串的子字符串的谓词

标签 ios sqlite core-data nspredicate nsfilemanager

在我的项目中,我在一个实体中有一组属性。其中一个是作为字符串的路径。我想要所有将我的字符串作为路径中的子路径的记录。

例子: 路径:/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/

相关文章:

ruby-on-rails - Rails Model.create SQLite3::SQLException

ios - 使用 iOS 社交/帐户框架检索 Twitter 用户数据

ios - 纹理渲染显得暗淡(openGL ES 2.0 iOS)

mysql - 外键的使用

ios - NSPredicate 与 NSNumbers 的 NSArray(核心数据)

ios - NSSet 上的核心数据一对多关系、分配、获取、谓词

ios - Magical Record 添加对象,不同上下文错误

ios - 快速获取字典中数组的值

ios - 蛇与梯子玩家位置保持从零开始

c++ - 无法打开sqlite数据库