我有 3 个核心数据实体。MenuItems
, Items
, ItemLangs
.
所有实体都有 iItemId
属性。用户需要使用 sItemName
进行搜索可以在 ItemLangs
中找到.
UITableView 包含 MenuItems
的数组.
我试过filteredArrayUsingPredicate,但我的谓词不正确。我是否需要遍历 MenuItems
的数组?首先在我可以过滤数组之前?
这是我的谓词:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"sItemName LIKE %@", searchText];
我可以知道如何使用它来过滤我拥有的 MenuItems 数组吗?
谢谢!
最佳答案
您需要的是这样的谓词:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sItemName CONTAINS[cd] %@", searchText];
[cd]
代表不区分大小写。或者,如果您喜欢“LIKE”,请参阅 this answer .
更新:
您还应该查看要获取的数据项 - 在您的情况下为 sItemName 。通过调试器,检查您的数据源数组返回什么对象?我指的是您要应用上述谓词的数组。也许有一些关键路径层次结构涉及到通过 sItemName,在这种情况下,您需要使用点表示法,例如
Items.ItemLangs.sItemName
.
关于iOS核心数据: Fetch child Magical Record data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25131097/