私はここで間違っていますか?すべてがうまく見えます。関数のシグネチャは正しい。私はparent
が特別な議論になる理由はないと思う。余分な引数、便利な初期設定子
import CloudKit
import CoreLocation
public enum OrderDirection {
case ascending, descending
}
open class OrderBy {
var key: String = ""
var direction: OrderDirection = .descending
var parent: OrderBy? = nil
open var sortDescriptor: NSSortDescriptor {
return NSSortDescriptor(key: self.key, ascending: self.direction == .ascending)
}
public convenience init(key: String, direction: OrderDirection, parent: OrderBy? = nil) {
self.init()
self.key = key
self.direction = direction
self.parent = parent
}
open func Ascending(_ key: String) -> Ascending {
return Ascending(key, parent: self)
}
open func Descending(_ key: String) -> Descending {
return Descending(key, parent: self)
}
open var sortDescriptors: [NSSortDescriptor] {
return self.parent?.sortDescriptors ?? [] + [self.sortDescriptor]
}
}
open class Ascending: OrderBy {
public convenience required init(key: String, parent: OrderBy? = nil) {
self.init(key: key, direction: .ascending, parent: parent)
}
}
open class Descending: OrderBy {
public convenience required init(key: String, parent: OrderBy? = nil) {
self.init(key: key, direction: .descending, parent: parent)
}
}
EDITは:実際のコードのスクリーンショット切り替え。
「昇順」メソッド名やクラス名ではなく、良いアイデア... –
@MartinR謝罪の両方です。私はそれを修正します。 – user1563544