【发布时间】:2017-10-15 15:54:16
【问题描述】:
我正在尝试使用NSSortDescriptor,因此使用 Swift 4 中的 Key Path 对结构列表进行排序。但是 NSSortDescriptor 的初始化在 NSSortDescriptor 中因 EXC_BAD_INSTRUCTION 而崩溃
struct Foo {
let bar : Int
let baz : String
}
let x = [Foo(bar: 1, baz: "Hello"),
Foo(bar: 2, baz: "World")]
let sortDescriptor = NSSortDescriptor(keyPath: \Foo.baz, ascending: true)
相关的堆栈跟踪如下所示。
#0 0x00000001007f97a0 in specialized _assertionFailure(_:_:file:line:flags:) ()
#1 0x0000000100ec5a12 in specialized static _KVOKeyPathBridgeMachinery._bridgeKeyPath(_:) ()
#2 0x0000000100e32142 in NSSortDescriptor.init<A, B>(keyPath:ascending:) ()
这让我觉得 NSSortDescriptor 不适用于非 KVO 对象,但是编译器既没有给我警告,NSSortDescriptor 的文档也没有提到关键路径需要用于暴露给目标的对象-C 运行时。
【问题讨论】: