如何使用swift 4排序方法使用#keyPath

问题描述:

我有一个nsmanaged对象类的数组。我想用特定元素对数组进行排序。如何使用swift 4排序方法使用#keyPath

每次我尝试使用它,它给了我 参数“#keyPath”的错误时

是指非 - “@ objc”属性“问题”无法将类型的价值“NSSortDescriptor”预期元素类型 'ServiceQuestion'

[NSSortDescriptor(key: #keyPath(objectClassArray), ascending: true)] 

let sortDescriptor = NSSortDescriptor(key: "last", ascending: true, selector: #selector(NSString.localizedStandardCompare)) 

使用特定的元素,而不是 “最后” 键

(people as NSArray).sortedArray(using: [sortDescriptor]) 

希望这会帮助你

+0

//返回NSArray(array:qaArray).sortedArray(使用:[NSSortDescriptor(key:#keyPath(Question.questionSequence),升序:true,selector:#selector(NSString.localizedStandardCompare)) ])为! [ServiceRequestQuestionAnswer] –

+0

我正在使用它,这是正确的方法 –

+0

看到我编辑的答案..它会有所帮助。 –