gpt4 book ai didi

swift - NSTableColumn 使用 Core Data 字典排序

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

型号

extension MyEntity {

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

@NSManaged public var customFields: NSDictionary?
}

customFields 键和值是字符串。

绑定(bind)

列是在代码中创建的,因为用户可以添加/删除列。

column.bind(NSBindingName.value, to: treeController!, withKeyPath: "arrangedObjects.customFields.\(uniqueID))", options: [.createsSortDescriptor:false])
column.sortDescriptorPrototype = NSSortDescriptor(key: "customFields.\(uniqueID))", ascending: true, selector: #selector(NSString.compare(_:)))

问题

单击列标题时排序将不起作用。值只是字符串,但似乎它们不被视为字符串。知道问题可能出在哪里吗?谢谢!

更新

  • NSTreeController 处于实体模式
  • 这是一个基于 View 的大纲 View (现在还用基于单元格的吗??)
  • 对于静态列(在 IB 中定义),排序有效
  • 当我对动态列进行排序时,没有任何反应(没有错误)
  • 我认为这些值不被视为字符串,因为它是一个不使用泛型的 NSDictionary

更新2

在另一个项目中,我成功地做到了这一点。唯一的区别是字典不是 ,但其中 MyObject 具有 String 类型的 value 属性。使用以下代码启用排序:

/* Bind column for sorting */
column.bind(NSBindingName.value, to: resultsArrayController!, withKeyPath: "arrangedObjects.dictionary.\(String(describing: paramDef.objectID)).value", options: nil)

或者这也有效:

column.sortDescriptorPrototype = NSSortDescriptor(key: "dictionary.\(String(describing: paramDef.objectID)).value", ascending: true, selector: #selector(NSString.compare(_:)))

我不知道为什么它在这种情况下有效,而不是在上面。

最佳答案

好吧,这非常愚蠢。典型的 KVO 拼写错误:

键:“customFields.\(uniqueID))”应该:键:“customFields.\(uniqueID)”

多了一个括号。我觉得我好笨...现在效果很好...

关于swift - NSTableColumn 使用 Core Data 字典排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589857/

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