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 being created by decoding archived data rather than by a normal constructor call. In iOS development, you encounter it most often when a view or view controller is loaded from a storyboard or nib, because Interface Builder uses an NSCoder to reconstruct the object graph.
So the short answer is: aDecoder or coder is the object that knows how to read the saved representation and rebuild the instance.
What coder Actually Is
NSCoder is an abstraction for encoding and decoding object state. During decoding, it provides the values needed to recreate the object.
A typical initializer looks like this:
The parameter name used to be written as aDecoder in older Objective-C and Swift examples. Modern Swift usually just calls it coder.
The meaning is the same: this parameter carries the archived data source for object reconstruction.
Why Storyboards Use It
When you instantiate a custom view from code, you usually call an initializer such as init(frame:). But when the same view is created from a storyboard or nib, iOS is decoding it from an archived interface file.
That means the system calls init(coder:) instead.
This pattern keeps initialization logic consistent whether the view comes from code or Interface Builder.
Why It Is Often required
In UIKit subclasses, init(coder:) is often marked required because the superclass requires that subclass implementations support decoding.
If the class might be created from a storyboard or nib and you do not implement this initializer correctly, the app can crash at runtime when the archived UI is loaded.
It Is Also Related to Archiving
Outside storyboards, NSCoder is also used in object archiving and unarchiving. Historically, classes could implement encoding and decoding to save and restore their state.
Example:
Here, coder is literally the object used to decode the archived property values.
Why People Get Confused
The name aDecoder sounds mysterious if you see it without context. It is not a special magic keyword. It is just an argument name from older Cocoa conventions.
This initializer is really saying: "build this object from the data that this coder can decode."
Once you see it that way, it becomes much less intimidating.
Common Pitfalls
- Thinking
aDecoderis a special language feature rather than just a parameter name for anNSCoderinstance. - Implementing
init(frame:)in a custom view but forgettinginit(coder:), then crashing when loading from a storyboard. - Duplicating setup logic instead of sharing it through a common initialization method.
- Calling the wrong superclass initializer inside
init(coder:). - Assuming
init(coder:)is only about storyboards when it also appears in archiving and unarchiving patterns.
Summary
- '
init(coder:)is used to initialize an object from decoded archived data.' - In storyboards and nibs, UIKit commonly creates objects through this initializer.
- '
aDecoderis just an older name for theNSCoderparameter.' - Custom views often need both
init(frame:)andinit(coder:), with shared setup logic. - The same idea also appears in object archiving and unarchiving, not just UI loading.
Related reading
- What exactly is init coder aDecoder?
- 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?
.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.