What exactly is init coder aDecoder?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
init(coder:) is the initializer used when an object is recreated from archived data instead of being built by your normal initializer. In Apple development, that usually means one of two things: a view or view controller is being loaded from a storyboard or XIB, or a model object is being decoded from an archive.
What init(coder:) Means
The coder parameter is an NSCoder instance, most commonly an NSKeyedUnarchiver, that knows how to read previously encoded values. When an object graph is unarchived, the system allocates the object and then calls init(coder:) so the object can restore its stored state.
That is why the method name looks a little unusual. It is literally “initialize this object using a decoder.” In older Objective-C examples the parameter was often named aDecoder, which is where the phrase comes from.
If an object is created in code with something like MyView(frame:) or MyType(name:), init(coder:) is not involved. It runs only when decoding happens.
A Minimal NSSecureCoding Example
The cleanest way to understand the initializer is to implement a tiny class that supports archiving and unarchiving.
Here, init(coder:) reads values back out of the archive and reconstructs the instance. Without it, unarchiving would fail because the class would not know how to restore itself.
Why UIKit Classes Require It
In UIKit and AppKit, storyboards and XIB files are archives. When Interface Builder saves a button, label, or custom view, that information is encoded. At runtime, the framework decodes it and calls init(coder:).
That is why custom views loaded from Interface Builder often need both a code initializer and a decoding initializer.
The shared commonInit() method avoids duplicating setup logic. That is a common pattern because both initialization paths should create the same usable view.
Why the Initializer Is Often required
You will usually see required init?(coder:) on classes meant to be subclassed. The required keyword means subclasses must also provide that initializer. This matters because the decoding system must be able to initialize the actual runtime type, not just the parent class.
The ? is there because decoding can fail. If a required field is missing or invalid, returning nil is a valid outcome.
When It Is Acceptable to Crash Intentionally
Sometimes a type is code-only and should never be loaded from a storyboard. In that case you will often see this pattern:
This is acceptable only when the type truly is not meant to support decoding. If the class might appear in a storyboard or nib, that crash will happen at runtime.
Common Pitfalls
The most common mistake is putting setup code only in init(frame:) and forgetting that storyboard-created instances come through init(coder:) instead.
Another mistake is decoding objects insecurely. Prefer NSSecureCoding and typed decode methods when possible.
Developers also sometimes assume init(coder:) is only for views. It is broader than that. Any archived object can use it.
Finally, do not leave fatalError in place for a class that Interface Builder instantiates. That is a guaranteed crash path.
Summary
- '
init(coder:)initializes an object from archived data' - in UIKit and AppKit, storyboard and XIB loading commonly use this initializer
- for custom archived objects,
init(coder:)restores properties from anNSCoder - '
requiredmeans subclasses must support the same decoding path' - if a class supports both code and storyboard creation, keep shared setup in one common initializer method
Related reading
- What goes into your .gitignore if you're using CocoaPods?
- What happens if my distribution certificate expires?
- What is a bundle in an Android application
- What is a good example to differentiate between fileprivate and private in Swift3
- What is a legit .gitignore for a Flutter project that is developed in Android Studio?
- What is a provisioning profile used for when developing iPhone applications?
- What is an Android PendingIntent?
- What is an Intent in Android?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.