Initializers in swift part-1 : (intro, convenience and designated intializers)

Initialization is a huge topic to cover in swift. I will try to simplify it as much as I can in this article.

Initializers

Initializers, are like special methods that can be called to create a new instance of a particular type.

init() {// perform some initialization here}
enum Gender {case male
case female
case unknown
}struct Human {var gender:Gender // this is a stored property.init(gender:Gender) {self.gender = gender}}let human = Human(gender: .male)
var gender:Gender = .male
var gender:Gender?

Customizing Initialization

struct Human {var gender:Gendervar age:Int = 10init(gender:Gender) { // initializer 1self.gender = gender}init(age:Int) { // initializer 2self.age = ageself.gender = .unknown}init(age:Int, gender:Gender) { // initializer 3self.age = ageself.gender = gender}}//------------------------------let human = Human(gender: .male)let human2 = Human(age: 20)let human3 = Human(age: 40, gender: .male)
let human4 = Human() // error :cannot invoke initializer for type ‘Human’ with no argumentslet human5 = Human(40,.male) //error:error: missing argument labels 'age:gender:' in call

Initializer Parameters Without Argument Labels

init(_ age:Int,_ gender:Gender) {self.age = ageself.gender = gender}
let human5 = Human(40,.male)

Default Initializers

class ShoppingListItem {var name: String?var quantity = 1var purchased = false}var item = ShoppingListItem()

Memberwise Initializers for Structure Types

struct Size {var width, height :Double // stored properties without default values}-------- or --------------
struct Size {
var width = 10.0, height = 30.0 // stored properties with default values}
let twoByTwo = Size(width: 2.0, height: 2.0)
struct Size {var width, height :Doubleinit(){
self.width = 10.0
self.height = 30.0
}}let sizeObj1 = Size(width: 2.0, height: 2.0)// error. argument passed to call that takes no argumentslet sizeObj2 = Size() // success.

Initializer Delegation for Value Types

struct Size {var width = 0.0, height = 0.0}struct Point {var x = 0.0, y = 0.0}
struct Rect {var origin = Point()var size = Size()init() {}init(origin: Point, size: Size) {self.origin = originself.size = size}init(center: Point, size: Size) {let originX = center.x - (size.width / 2)let originY = center.y - (size.height / 2)self.init(origin: Point(x: originX, y: originY), size: size)}}

Designated Initializers and Convenience Initializers

init( _parameters if any_ ) {
}

You do not have to provide convenience initializers if your class does not require them. Create convenience initializers whenever a shortcut to a common initialization pattern will save time or make initialization of the class clearer in intent.

convenience init( _parameters if any_ ) {
}
class HumanBeing {var name: Stringinit(name: String) {self.name = name}convenience init() {self.init(name: “not set”)// Convenience init call the designated init method}}let humanBeingObj1 = HumanBeing() // calls convenience initlet humanBeingObj2 = HumanBeing(name: “abhilash”) // calls designated init

Initializer Delegation for Class Types

Image for post
Image for post
source: apple docs

Automatic Initializer Inheritance

class HumanBeing {
var name: String
init(name: String) {
self.name = name
}
convenience init() {
self.init(name: “not set”)
// Convenience init call the designated init method
}
}

let humanBeingObj1 = HumanBeing() // calls convenience init
let humanBeingObj2 = HumanBeing(name: “abhilash”) // calls
designated init

____________________
class Man: HumanBeing {
var age:Int = 0
override init(name: String) {
super.init(name: name)
}
init(name: String, age:Int) {
super.init(name: name)
self.name = name
self.age = age
}
}
_______________________let manObj1 = Man() // calls convenience init of Human class
let manObj2 = Man(name: “Robert”) // calls overriden init
let manObj3 = Man(name: “John”, age: 10) // calls custom init
manObj1.name // prints “not set”
manObj2.name // prints “Robert”
manObj3.name // prints “John”

If you enjoyed reading this post and found it useful, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store