gpt4 book ai didi

Swift CoreData 将谓词与数组一起使用

转载 作者:行者123 更新时间:2023-11-28 05:51:50 25 4
gpt4 key购买 nike

我需要在 CoreData 中保留字符串数组,然后使用谓词为该数组过滤数据。

根据这篇帖子字段类型 Transformable(带有自定义类 [String])可以解决问题,我可以正确保存字符串数组: How to save Array to CoreData?

但是,我不能对这个字段使用谓词。具体来说,我的目标是找到所有项目,其中保存的数组中至少有一项与模式匹配。

所以,回到问题:有没有一种在 CoreData 中存储数组的好方法,以便我以后可以为它应用谓词?

最佳答案

CoreData 找到了合适的解决方案

不要将数组作为 Entity 中的 Transformable 字段

  1. 使用单个字段创建单独的实体,使用该实体而不是数组
  2. 在主 Entity 和数组替换 Entity 之间创建关系。
  3. 在运行时:使用谓词查找数组 EntityCoreObject:
NSPredicate(
format: "%K = %@", #keyPath(YourArrayEntity.onlyField),
matchingString
)
  1. 使用谓词搜索您的主要实体
NSPredicate(
format: "ANY %K == %@",
#keyPath(YourMainEntity.relationToArrayEntity),
arrayEntityFoundInPreviousStep
)

关于Swift CoreData 将谓词与数组一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com