Swift create custom initializer without parameters with custom name

You need to create a static method:

static func initWithSomeMode() -> YourClass {
    let obj = YourClass()
    obj.someCustomSetup()
    return obj
}

And then you can do this:

let yourClass = YourClass.initWithSomeMode()

Also a workaround, but I tend to use this:

convenience init(withSomeMode: Void) {
    self.init()
    self.customSetup()
}

and then:

YourClass.init(withSomeMode: ())

it has some advantages over static method, like easier subclassing. But yes, still a workaround.

Tags:

Swift