I want to create init for my class that might look something like this:
initWithSomeMode { // Not compilable
self.init()
self.customSetup()
}
Of course code above will not work, I just want to show what I want to achieve.
I can only see convenience init in Swift class, but in that case I need to add parameters, but I don't need them.
So, I might achieve this with convenience init something like this:
convenience init(foo: String?) {
self.init()
self.customSetup()
}
Is there any way to create custom init without parameters?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With