gpt4 book ai didi

ios - Core Data 对子数组 NSSet 进行排序

转载 作者:行者123 更新时间:2023-11-30 12:43:01 24 4
gpt4 key购买 nike

我尝试对实体内的数组 (vociMenu: NSSet) 进行排序,但收到此错误:

'NSInvalidArgumentException', reason: 'to-many key not allowed here'

这是我的代码:实体

extension SezioniMenu {

@nonobjc public class func fetchRequest() -> NSFetchRequest<SezioniMenu> {
return NSFetchRequest<SezioniMenu>(entityName: "SezioniMenu");
}

@NSManaged public var sezione: String?
@NSManaged public var language: Language?
@NSManaged public var vociMenu: NSSet? <---- /// I MUST ORDER THIS ENTITY

}

查询

        let fetchRequest: NSFetchRequest<SezioniMenu> = SezioniMenu.fetchRequest()
fetchRequest.returnsObjectsAsFaults = false

let sortDescriptor = NSSortDescriptor(key: "vociMenu.tipologia", ascending: true)

let sortDescriptors = [sortDescriptor]
let predicate = NSPredicate(format: "language.code = %@", Singleton.sharedInstance.linguaDispositivo)

fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDescriptors
do {
array = try self.managedObjectContext.fetch(fetchRequest)
.....

如何订购实体“vociMenu: NSSet”?

感谢您的帮助

更新

我设法通过这种方式解决我的情况。直接在TableView的委托(delegate)中对列表进行排序:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
///For each section retrieve the list to order
let list = self.listSezioni?[indexPath.section].vociMenu?.allObjects as! [VociMenu]
///I run the sort (sort) of the items
var listSort = list.sorted(by: { $0.tipologia! < $1.tipologia! })

cell.labelMenu.text = listSort[indexPath.row].tipologia
}

最佳答案

我不知道您的 vociMenu.tipologia 字段的类型是什么,但问题可能出在使用 keyPathes 而不仅仅是键上。尝试使用 sortDescriptor,而不是由

创建
public init(key: String?, ascending: Bool, comparator cmptr: @escaping Foundation.Comparator)

您可以在其中明确提供比较逻辑。希望这有帮助

关于ios - Core Data 对子数组 NSSet 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023126/

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