1
我试图使用NSSortDescriptor
所以排序斯威夫特4.使用键路径结构的列表但NSSortDescriptor
崩溃与EXC_BAD_INSTRUCTION
在NSSortDescriptor
斯威夫特关键路径导致EXC_BAD_INSTRUCTION在NSSortDescriptor
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不与非志愿对象的工作,但无论是编译器给我一个警告,也没有NSSortDescriptor的文件提到的关键路径需要一个对象,它是暴露于Objective-C运行时。
感谢您的回答!那么NSSortDescriptor需要Objective-C运行时?我有点期待,但仍然是一个无赖。我真的很想将它用于结构。 –
@ThomasBartelmess:是的。你也可以只将它应用于'NSArray',所以它对本地Swift类型的使用非常有限。 –