init(coder:)Returns an object initialized from data in a given unarchiver. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+ Required.
The NSCoding protocol requires that you implement the required initializer init?(coder aDecoder: NSCoder). This initializer is what allows you to unarchive the ViewController using aDecoder object.
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.
The requirement to implement that initializer is a consequence of two things:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With