NSPREDICATE不在Swift(Coredata)上工作
我有一个具有1个属性(int)的实体,并且与另一个实体有关系。我创建了一个具有属性和关系的对象,并将其添加到数据库中。如果我获取实体中的所有对象,我可以看到它已经创建了,但是我不能将NSPRedicate与通讯属性和关系一起使用。
fetchRequest.predicate = NSPredicate(format: "(int_attribute == %@) AND (relation_entity like %@)", int_value, relation_object)
let list = (try? context.fetch(fetchRequest)) ?? []
执行此操作后,列表为空。如果我只使用int_attribute使用nspredicate,也会发生同样的情况。 上下文是从ObservableObject的类datacontroller的引用,其中我具有loadSistentStores的nspersistentContainer和init()函数。
I have an entity with 1 attribute (Int) and a relation with another entity. I've created an object with an attribute and a relation and added it to the database. If I fetch all the objects in the entity I can see this one has been created, but I can't using NSPredicate with the correspondent attribute and relation.
fetchRequest.predicate = NSPredicate(format: "(int_attribute == %@) AND (relation_entity like %@)", int_value, relation_object)
let list = (try? context.fetch(fetchRequest)) ?? []
After doing this, list is empty. And the same happens if I use NSPredicate with just int_attribute.
Context is a reference to a class DataController from ObservableObject where I have a NSPersistentContainer and init() function with loadPersistentStores.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在谓词中,
%@
期望对象。这不是一个通用的“替换此”指标,并且对于不同的数据类型有不同的指示符。如果您要匹配原始类型,则需要其他东西。对于整数,尝试%d
。对于其他事情,请查找printf
printf stypyle form-style format cormat specifiers 。In predicates,
%@
expects an object. It's not a general "replace this" indicator, and there are different specifiers for different data types. If you're matching primitive types you need something else. For integers, try%d
. For other things, look upprintf
-style format specifiers.