131

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?

pkamb
  • 26,648
  • 20
  • 124
  • 157
JasonP
  • 1,485
  • 3
  • 8
  • 9
  • 5
    This answer will help you http://stackoverflow.com/questions/24036393/fatal-error-use-of-unimplemented-initializer-initcoder-for-class Thank you – Seungyoun Yi Jul 15 '16 at 01: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 '16 at 01:00

2 Answers2

128

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.

Lukas Würzburger
  • 6,207
  • 7
  • 35
  • 68
Code Different
  • 73,850
  • 14
  • 125
  • 146
  • Why not put everything inside awakeFromNib and forget using `init(coder aCoder : NSCoder)`? – Honey Jan 16 '17 at 21:39
  • @Honey - in a word, "sometimes you can't do that". You typically can but not always. – Fattie Mar 30 '17 at 13:28
  • 1
    @Fattie are the details of not doing it too complex or unnecessary to know? If not do you mind explaining? – Honey Mar 30 '17 at 13:53
  • hey @Honey - i just dropped in a note, no time for an explanation! just go ahead and ask a new Q on here, it's a good question. you'll get huge detailed answers. – Fattie Mar 30 '17 at 16:04
  • 9
    @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) – Code Different Jul 28 '17 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` – Code Different Jul 28 '17 at 15:07
  • Could you explain the syntax of `init(coder aDecoder : NSCoder)`? So the parameter is a coder of type `aDecoder`, which implements `NSCoder`? – Rainning Nov 23 '20 at 10:16
  • @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 }. – Andy Weinstein Dec 31 '20 at 11:08
30

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.

Community
  • 1
  • 1
Alexander
  • 48,074
  • 8
  • 78
  • 121
  • 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. – Nicolas Miari Jul 15 '16 at 01: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](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID222) – TheBaj Jan 30 '17 at 15:20