NSPREDICATE不在Swift(Coredata)上工作

发布于 2025-01-24 22:14:33 字数 486 浏览 0 评论 0原文

我有一个具有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2025-01-31 22:14:34

在谓词中,%@期望对象。这不是一个通用的“替换此”指标,并且对于不同的数据类型有不同的指示符。如果您要匹配原始类型,则需要其他东西。对于整数,尝试%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 up printf-style format specifiers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文