调用中的额外参数,便利初始化器

Extra argument in call, Convenience Initializers

我在这里做错了什么?一切似乎都很好。函数签名是正确的。我看不出为什么 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)
    }
}

编辑:从屏幕截图切换到实际代码。

看起来该函数正在尝试调用自身,因此它不期望 Ascending/Descending class 的初始化中存在 parent 参数。我建议更改函数的名称,以便调试器更清楚您希望使用哪个函数。将第一个字母改为小写就可以了。

如果它们具有相同的范围,我认为您不会遇到相同的问题。

方法名以小写开头!!!

修复:

open func ascending(_ key: String) -> Ascending {
    return Ascending(key: key, parent: self)
}

open func descending(_ key: String) -> Descending {
    return Descending(key: key, parent: self)
}