Initializer
Initializers are called when we initialize a new instance of a particular type (class or structure). Initialization is the process of preparing an instance for use. The initialization process can include setting initial values for stored properties, verifying resources, such as web services, files, and so on are available or setting up the UI properly. Initializers are generally used to ensure that the instance of the class or structure is properly initialized prior to first use.
Initializers are special methods that are used to create a new instance of a type. We define an initializer exactly as we would define other methods, but we must use the init
keyword as the name of the initializer to tell the compiler that this method is an initializer. In its simplest form, the initializer does not accept any arguments. Let's look at the syntax used to write a simple initializer:
init() { //Perform initialization here }
This format works for both classes and structures. By default, all...