swiftconvenience-methods

Extra argument in call, Convenience Initializers


What am I doing wrong here? Everything seems fine. Function signatures are correct. I see no reason why the parent would be an extra argument.

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: Switched from a screenshot to actual code.


Solution

  • It looks like the function is trying to call itself, so it is not expecting the parent parameter that is present in the init for the Ascending/Descending class. I suggest changing the name of the function so that it is more clear to the debugger which function you are wishing to use. Changing the first letter to lower case should suffice.

    I don't think you would be having the same issue if they had the same scope.