0

模型

extension MyEntity {

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

    @NSManaged public var customFields: NSDictionary?
}

customFields 键和值是字符串。

绑定

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

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实体模式
  • 这是一个基于视图的大纲视图(现在仍然使用基于单元格的视图吗??)
  • 对于静态列(在 IB 中定义),排序有效
  • 当我对动态列进行排序时,什么也没有发生(没有错误)
  • 我想这些值不被视为字符串,因为它是一个不使用泛型的 NSDictionary

更新 2

在另一个项目中,我成功地做到了。唯一的区别是字典不是,而是 whereMyObjectvaluetype 的属性String。使用以下代码启用排序:

/* 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(_:)))

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

4

1 回答 1

0

好吧,这很愚蠢。一个典型的 KVO 错字:

key: "customFields.\(uniqueID))" 应该: key: "customFields.\(uniqueID)"

有一个额外的括号。我觉得很愚蠢......这现在完美无缺......

于 2020-01-06T15:51:58.473 回答