Classes and Initializers

This last week I have spent some time getting back into Swift and learning a little about classes, structs, and initializers. Classes and structs are very similar. Both allows you to store information, run functions, and define initializers. With classes you have to define your own initializers. Classes are a bit more powerful than Structs, but I will get into that in a later post. There are two kinds of initializers that you can use with classes. These are the designated initializers and the convenience initializers.

Designated Initializers 

Designated Initializers must be used when creating an instance of a class. These are the main initializer that is used with classes, but they are not the only ones that you have to use. These initializers can take parameters, and are pretty much the same as the initializers that you would use with structs. The difference between the two happens when you create an instance. To create a designated initializer you create your class, then before closing the brackets write the initializer. It starts with the word init, then it has parenthesis with the parameters inside. After that you have your curly brackets and code. Within the code you use the word ‘self’ followed by ‘.’ and the name of the parameters ‘=’ the parameter name.

Ex: 
Class message {
    let sender: String
    let recipient: String

init(sender: String, recipient: String) {
    self.sender = sender
    self.recipient = sender
}
}

Now, in all fairness, there may be slightly different structures for the code inside, but this seems like it is consistent. I am still trying to get my head around this.

Convenience Initializers 

Convenience initializers are as their name states, more convenient. They have more of the setup done for you so you have less hard coding to do. Ultimately these initializers must call the designated initializers. These initializers are often used to set defaults for special use cases. Convenience initializers start with the term convenience, then they have the word init followed by the parameters in parenthesis. Within the curly braces you then have the word init again and parenthesis with the parameters from the init designated statement.

Ex:
convenience init(sender: String) {
    self.init(sender: sender, recipient: sender)
}

*for this code it is showing the sender under recipient and sender because you are sending a message to yourself. 

Rules for initializers

Apple calls out a few specific rules about these initializers. These rules are taken directly from Apple.

  1. A designated initializer must call its designated initializer from its super class.
  2. A convenience initializer must call another initializer from the same class.
  3. A convenience initializers must ultimately call a designated initializer.

This is just a brief introduction to designated initializers and convenience initializers. If you want more information then I highly recommend reading through this post at codingexplorer.com or reading through the Programming Language Guide from Apple.