Swift Initialize Struct with optional stored properties

前端 未结 3 1146
终归单人心
终归单人心 2020-12-13 12:32

I\'m a Swift newbie and I\'m trying to get my head around using Structs with optional properties. I\'ve done quite a bit of searching and got something that works but it fee

3条回答
  •  猫巷女王i
    2020-12-13 13:16

    Use default values:

    init(busName: String, website: String? = nil, address: String? = nil) {
        self.name = busName
        self.web = website
        self.address = address
    }
    

    Then you can call the init like this:

    _ = Business(busName: "Foo")
    _ = Business(busName: "Foo", website: "www.foo.bar")
    _ = Business(busName: "Foo", address: "bar")
    _ = Business(busName: "Foo", website: "www.foo.bar", address: "bar")
    

提交回复
热议问题