initWithSomething is an Objective-C style, and it will not work exactly the same as in Swift.
In Swift, all initializers are called init and may optionally accept some parameters.
You can use default values, for example:
class Example { var aVar: String init(goodVar : String = "Good Var!") { aVar = goodVar } } let example1 = Example() println(example1.aVar)
However, this approach will not work exactly the same as in your example, because as soon as you enter the second init method, it is ambiguous that you wanted:
class Example { var aVar: String init(goodVar : String = "Good Var!") { aVar = goodVar } init(fooBar : String = "Foo Bar") { aVar = fooBar } } let example1 = Example()
A better approach might be to use Swift enum :
class Example { enum ExampleType : String { case GoodVar = "Good Var!" case FooBar = "Foo Bar" } var aVar: String init(type : ExampleType) { aVar = type.rawValue } } let example1 = Example(type: .GoodVar) println(example1.aVar) // "Good Var!" let example2 = Example(type: .FooBar) println(example2.aVar) // "Foo Bar"
This approach will provide the necessary flexibility using the usual Swift syntax.
source share