gpt4 book ai didi

ios - 如何在 Swift 中的 collectionView 中显示每个类别的多个类别?

转载 作者:行者123 更新时间:2023-11-29 05:47:04 30 4
gpt4 key购买 nike

我需要在 Collection View 中为每个类别显示多个类别,因为在 CoreData 中,一个类别有多个类别,并且类别设置为 NSset 类型当写这行时

fetchRequest.predicate = NSPredicate(format: "category.categories == %@", (category?.categories!)!)

它抛出一个错误CoreData:错误:SQLCoredispatchRequest:异常处理请求:,此处不允许使用 userInfo 为(null)的对多键

最佳答案

每次需要在获取时进行过滤时,您不必使用 NSPredicate 的格式字符串(但这是最便宜的解决方案 - 请记住)。

您可以使用predicateWithBlock初始化器:

fetchRequest.predicate = NSPredicate { object, bindings -> Bool in
guard let object = object as? Category else { return false }
return object.categories == category?.categories
}

或者甚至Array.filter:

guard let allCategories = try? moc.executeFetchRequest(fetchRequest) as? [Category] else { return }
let result = allCategories.filter { $0.categories == category?.categories }

关于ios - 如何在 Swift 中的 collectionView 中显示每个类别的多个类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56040963/

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