Class has no initializers Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When Swift shows a message that a class has no initializers, the compiler is telling you that at least one stored property can be left uninitialized when an instance is created. Swift enforces initialization rules very strictly so object state is valid from the first line after initialization. The fix is usually to provide default values, add a designated initializer, or adjust inheritance setup.
Why This Error Happens
Swift requires every stored property in a class to have a value before init returns. The compiler checks all code paths, not just the common one. If even one path can exit without assigning every property, the class is rejected.
A common failure pattern is introducing a non optional property and forgetting to update the initializer:
In this code, expiresAt has no default and no assignment in init. Swift cannot synthesize a safe initializer, so you get the error.
Fix Option 1: Provide Default Values
If a property has a reasonable fallback, assign it at declaration time. This removes boilerplate and often improves clarity.
Use defaults only when they represent a valid state. Do not hide missing required data behind placeholder values that break later logic.
Fix Option 2: Write a Designated Initializer
When every property is required, define a designated initializer that sets all stored properties.
Designated initializers should establish complete object state. Convenience initializers can call into them for easier call sites.
Inheritance Rules That Trigger This Error
Initialization becomes stricter in class hierarchies. A subclass must initialize its own properties before calling super.init, then ensure the parent class is correctly initialized.
If Employee forgets to assign employeeId, or calls super.init first, the compiler rejects it. Swift enforces this order so partially initialized instances never escape.
Optional and Lazy Properties
Optional properties are initialized to nil by default, so they do not force extra initializer parameters. lazy properties are initialized at first access, which can also reduce initializer complexity.
Use these tools intentionally. If cachedData is actually required for correctness, making it optional only delays a failure.
A Practical Refactor Pattern
If initializer signatures keep growing, move related values into a dedicated type and pass one value object. This keeps class initialization readable without weakening guarantees.
This approach keeps object creation explicit and avoids fragile chains of convenience initializers.
Common Pitfalls
- Adding a new non optional stored property and forgetting to update every designated initializer.
- Assigning parent properties directly in a subclass instead of calling
super.initwith required arguments. - Using placeholder defaults for required business fields, which removes the compiler error but introduces runtime bugs.
- Marking too many fields optional to silence initialization errors, then scattering force unwraps in later code.
- Creating convenience initializers that do not funnel to one designated initializer, leading to duplicated and inconsistent setup logic.
Summary
- Swift requires all stored properties to be initialized before object creation finishes.
- Default values are useful when they represent a valid initial state.
- Designated initializers should define complete, correct state in one place.
- Inheritance has strict ordering rules for subclass and parent initialization.
- Optional and lazy properties help reduce initializer complexity, but they should reflect real semantics.

