grominet
grominet

Reputation: 272

How have multiple init() with Swift

Is it possible, if yes how, to have multiple init without parameter, in a Class like this (the string is just an exemple):

aVar: String

init() {
   aVar = "simple init"
}

initWithAGoodVar() {
   aVar = "Good Var!"
}

initWithFooBar() {
   aVar = "Foo Bar"
}

Upvotes: 9

Views: 20898

Answers (4)

Aaron Brager
Aaron Brager

Reputation: 66242

The initWithSomething is an Objective-C style, and it won't work exactly like that in Swift.

In Swift, all initializers are called init and can optionally take some parameters.

You could use default values like this:

class Example {
    var aVar: String

    init(goodVar : String = "Good Var!") {
        aVar = goodVar
    }
}

let example1 = Example()
println(example1.aVar) // prints "Good Var!"

However, this approach won't work exactly as in your example, because once you introduce the second init method, it's ambiguous which you wanted:

class Example {
    var aVar: String

    init(goodVar : String = "Good Var!") {
        aVar = goodVar
    }

    init(fooBar : String = "Foo Bar") {
        aVar = fooBar
    }
}

let example1 = Example() // error: cannot invoke initializer…

A better approach might be to use Swift's 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 flexibility you want using normal Swift syntax.

Upvotes: 4

Mikael Hellman
Mikael Hellman

Reputation: 2724

Its not possible to have multiple init, taking the same (or no) params.

But if you are looking for a way to construct objects with predefined states, you could use static factory methods:

class Test {

  let aVar: String

  init(aVar: String) {
    self.aVar = aVar
  }

  static func initWithAGoodVar() -> Test {
    return Test(aVar: "Good Var!")
  }

  static func initWithFooBar() -> Test {
    return Test(aVar: "Foo Bar")
  }
}

let test = Test.initWithFooBar()
println(test.aVar) // Prints: "Foo Bar"

Upvotes: 2

ABakerSmith
ABakerSmith

Reputation: 22939

You cannot have multiple inits without parameters since it would be ambiguous which init method you wanted to use. As an alternative you could either pass the initial values to the init method. Taking the example from your question:

class MyClass {
    var myString: String

    init(myString: String) {
        self.myString = myString
    }
}

let a = MyClass(myString: "simple init")

Or, if myString is only supposed to be certain values, you could use an enum:

class MyOtherClass {
    enum MyEnum: String {
        case Simple  = "Simple"
        case GoodVar = "GoodVar"
        case FooBar  = "FooBar"
    }

    var myString: String

    init(arg: MyEnum) {
        myString = arg.rawValue
    }
}

let b = MyOtherClass(arg: .GoodVar)
println(b.myString) // "GoodVar"

Upvotes: 6

Stefan Scoarta
Stefan Scoarta

Reputation: 791

You can have different init with different parameters.When you create a new instance of your class,the init method is called with the arguments you passed in

Upvotes: 1

Related Questions