What exactly is init coder aDecoder?

I'm learning iOS development from an online course and everytime I make a custom view (custom table view cell, collection view cell, etc) the instructor always implements this initializer:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

Why exactly do I always have to call this? What does it do? Can I put properties inside the init?


Solution 1:

I'll start this answer from the opposite direction: what if you want to save the state of your view to disk? This is known as serialization. The reverse is deserialization - restoring the state of the object from disk.

The NSCoding protocol defines two methods to serialize and deserialize objects:

encodeWithCoder(_ aCoder: NSCoder) {
    // Serialize your object here
}

init(coder aDecoder: NSCoder) {
    // Deserialize your object here
}

So why is it needed in your custom class? The answer is Interface Builder. When you drag an object onto a storyboard and configure it, Interface Builder serializes the state of that object on to disk, then deserializes it when the storyboard appears on screen. You need to tell Interface Builder how to do those. At the very least, if you don't add any new properties to your subclass, you can simply ask the superclass to do the packing and unpacking for you, hence the super.init(coder: aDecoder) call. If your subclass is more complex, you need to add your own serialization and deserialization code for the subclass.

This is in contrast to the Visual Studio's approach, which is to write code into a hidden file to make the object at run time.

Solution 2:

The requirement to implement that initializer is a consequence of two things:

  1. The Liskov substitution principle. If S is a subclass of T (e.g. MyViewController is a subclass of ViewController), then S objects (instances of MyViewController) must be able to be substituted in where T objects (instances of ViewController) are expected.

  2. Initializers are not inherited in Swift if any initializers are explicitly defined in the subclass. If one initializer is explicitly provided, then all others must be explicitly provided (which can then just call super.init(...)). See this question for rationale. It's in Java, but still applies.

By point 1, everything the original ViewController can do, the MyViewController subclass should be able to do. One such thing is to be able to be initialized from a given NSCoder. By point 2, your MyViewController subclass doesn't automatically inherit this ability. Thus, you must manually supply the initializer that fulfills this requirement. In this case, you just need to delegate up to the superclass, to have it do what it would usually do.