140

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?

2
  • 5
    This answer will help you stackoverflow.com/questions/24036393/… Thank you
    – Kyle Yi
    Jul 15, 2016 at 1:00
  • 2
    If you subclass an object that implements NSCoding then you need to implement this initialiser, since it is required of classes that implement NSCoding. You must at least call the superclass init method. If the NSCoder contains encoded properties for your class then you can use this method to recover those
    – Paulw11
    Jul 15, 2016 at 1:00

3 Answers 3

143

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.

8
  • Why not put everything inside awakeFromNib and forget using init(coder aCoder : NSCoder)?
    – mfaani
    Jan 16, 2017 at 21:39
  • 1
    @Fattie are the details of not doing it too complex or unnecessary to know? If not do you mind explaining?
    – mfaani
    Mar 30, 2017 at 13:53
  • 10
    @Honey if you want to configure your object in Interface Builder then awakeFromNib won't work. awakeFromNib is invoked at run time. Anything you do in Interface Builder is during design time. To carry what you have done in design time to run time is encodeWithCoder (saving) and init(coder:) (loading) Jul 28, 2017 at 15:05
  • 3
    @Honey if you don't use Interface Builder to configure your custom class (i.e. do it programmatically with code) then you can do it in awakeFromNib or initWIthFrame Jul 28, 2017 at 15:07
  • 1
    @Raining The syntax is standard Swift function header syntax. "coder" is the name of the parameter that any code that calls this function will be required to use. If you don't want the caller to have to name the param, you write "_" instead of "coder". "aDecoder" is how the value passed in will be referred to inside the body of the function. The ":" means that the next thing is a variable type. "NSCoder" is a variable type. To call this function, you call - init(coder: myObjectInstanceOfAnNSCoder). In the body of the function, you write - if aDecoder != someOtherObject { do something }. Dec 31, 2020 at 11:08
34

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.

2
  • 1
    It makes perfect sense that constructors not be inherited: If you initialize an instance of the derived class using the (inherited) initializer of the base class, the non-inherited properties that were newly defined ("added") by the derived class will never be initialized. Jul 15, 2016 at 1:58
  • 3
    Actually, initializers are inherited in Swift, given you do not provide any of your own initializer implementations in your subclass. If your newly defined non-inherited properties have default values, you can get away with not writing any initializers in your subclass and simply inherit all your superclass' initializers. See here
    – TheBaj
    Jan 30, 2017 at 15:20
-2

When you create a custom subclass of UIView and use it in a storyboard or nib file, Xcode automatically generates an XML file that represents the contents of the storyboard or nib file. When your app runs, the XML file is read and the encoded objects are decoded using the NSCoder protocol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.