How to initialize classes in Swift?

Classes in Swift must initialize all of their stored properties before an instance of class is ready for use. This can be done using initializers or assigning default initial values to the stored properties.

Initializers

Initializers are called to create new instance of particular type. They are like instance method without parameter name written using init keyword.

Using Default Property Values

We can also initialize classes by assigning default values to their stored properties. This can be done by specifying default property value as a part of property’s declaration.

Leave a Reply