How have multiple init() with Swift

杀马特。学长 韩版系。学妹 提交于 2019-12-08 18:48:37

问题


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"
}

回答1:


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"



回答2:


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"



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/30550640/how-have-multiple-init-with-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!