0

ここで何が間違っていますか?すべて問題ないようです。関数のシグネチャは正しいです。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)
    }
}

編集: スクリーンショットから実際のコードに切り替えました。

4

2 に答える 2

1

関数が自分自身を呼び出そうとしているように見えるparentため、Ascending/Descending クラスの init に存在するパラメーターを想定していません。関数の名前を変更して、どの関数を使用したいのかがデバッガーに明確になるようにすることをお勧めします。最初の文字を小文字に変更するだけで十分です。

スコープが同じであれば、同じ問題が発生することはないと思います。

于 2017-11-03T06:37:23.647 に答える