Can I use the default initializer for a class in Swift?

Let's say I have a class Foo:

class Foo {
   let bar: String
   let baz: String
}

Do I need to manually write the initializer in order?

init(bar: String, baz: String) {
    self.bar = bar
    self.baz = baz
}

Its a clean template.

+4
source share
1 answer

If you can work with value semantics instead of reference semantics, you can use Struct instead of a class, they have automatic default initializers.

enter image description here

If not, then you must provide full init for your class.

+2
source

Source: https://habr.com/ru/post/1611262/


All Articles