Swift error: return from initializer without initializing all stored properties
The Swift compiler error return from initializer without initializing all stored properties occurs when an initializer finishes before assigning a value to every stored property that requires one.
Swift requires an instance to be fully initialized before the initializer returns. A stored property satisfies this rule when it is assigned inside init, has a default value in its declaration, or is an optional property that defaults to nil.
Why Swift reports that a stored property is not initialized
In the following structure, brand, name, and capacity are stored properties. The initializer assigns values to only brand and name. Because capacity is a non-optional Int without a default value, Swift cannot create a valid instance.
Let us recreate this scenario. We shall define a structure with three properties but only two of them are initialized in init method.
main.swift
struct car {
var brand: String
var name: String
var capacity: Int
init(brand: String, name: String) {
self.brand = brand
self.name = name
}
}
let car1 = car(brand: "Hyundai", name: "Creta")
print("Car details\n-----------")
print("Brand is \(car1.brand)")
print("Name is \(car1.name)")
print("Seating capacity is \(car1.capacity)")
Compiler error
main.swift:9:4: error: return from initializer without initializing all stored properties
}
^
main.swift:4:8: note: 'self.capacity' not initialized
var capacity: Int
^
The compiler note identifies the property that has not received a value. In this example, the missing property is self.capacity.
How to fix return from initializer without initializing all stored properties
Choose the fix that matches the meaning of the property:
- Pass a required value to the initializer and assign it.
- Declare a sensible default value for the property.
- Make the property optional when the absence of a value is valid.
- Ensure every conditional path through the initializer assigns the property.
Solution 1: Initialize every stored property in init
Use this approach when callers must provide a value for the property. Add capacity as an initializer parameter and assign it to self.capacity before the initializer returns.
main.swift
struct car {
var brand: String
var name: String
var capacity: Int
init(brand: String, name: String, capacity: Int) {
self.brand = brand
self.name = name
self.capacity = capacity
}
}
let car1 = car(brand: "Hyundai", name: "Creta", capacity: 5)
print("Car details\n-----------")
print("Brand is \(car1.brand)")
print("Name is \(car1.name)")
print("Seating capacity is \(car1.capacity)")
Output
Car details
-----------
Brand is Hyundai
Name is Creta
Seating capacity is 5
Solution 2: Give the stored property a default value
Use a default value when the same value is appropriate for most or all new instances. A property initialized in its declaration does not need to be assigned again in init.
main.swift
struct car {
var brand: String
var name: String
var capacity = 5
init(brand: String, name: String) {
self.brand = brand
self.name = name
}
}
let car1 = car(brand: "Hyundai", name: "Creta")
print("Car details\n-----------")
print("Brand is \(car1.brand)")
print("Name is \(car1.name)")
print("Seating capacity is \(car1.capacity)")
Output
Car details
-----------
Brand is Hyundai
Name is Creta
Seating capacity is 5
Solution 3: Make the property optional when no value is valid
An optional stored property automatically starts with the value nil. This is suitable only when the property may genuinely be unavailable. Do not make a property optional merely to silence the compiler.
struct Car {
var brand: String
var name: String
var capacity: Int?
init(brand: String, name: String) {
self.brand = brand
self.name = name
}
}
let car = Car(brand: "Hyundai", name: "Creta")
print(car.capacity as Any)
Output
nil
Initialize stored properties on every conditional path
The same compiler error can occur even when a property appears to be assigned inside init. Every possible path through the initializer must assign it.
In this incorrect example, category is initialized only when capacity is greater than four. The initializer can also follow the path where the condition is false, leaving category without a value.
struct Car {
var capacity: Int
var category: String
init(capacity: Int) {
self.capacity = capacity
if capacity > 4 {
self.category = "Family car"
}
}
}
Add an else branch so that category receives a value in both cases.
struct Car {
var capacity: Int
var category: String
init(capacity: Int) {
self.capacity = capacity
if capacity > 4 {
self.category = "Family car"
} else {
self.category = "Compact car"
}
}
}
Stored property initialization in Swift classes
The initialization rule also applies to classes. A class must initialize all stored properties introduced by that class before completing initialization. For a subclass, its own stored properties must be initialized before it delegates to the superclass initializer with super.init.
class Vehicle {
var brand: String
init(brand: String) {
self.brand = brand
}
}
class Car: Vehicle {
var capacity: Int
init(brand: String, capacity: Int) {
self.capacity = capacity
super.init(brand: brand)
}
}
Assigning capacity before calling super.init follows Swift’s two-phase class initialization rules.
Common causes of incomplete Swift initialization
- A new stored property was added, but an existing custom initializer was not updated.
- A property is assigned in an
ifbranch but not in its matchingelsebranch. - An initializer returns early before every property receives a value.
- A subclass calls
super.initbefore initializing its own stored properties. - A non-optional property was intended to have a default value, but none was declared.
Swift initializer error checklist
- Check the compiler note for the exact property marked as not initialized.
- Verify that every non-optional stored property has a value before
initfinishes. - Review every
if,switch, and early-return path in the initializer. - Confirm that declared default values are appropriate for the data model.
- Use an optional type only when
nilrepresents a valid state. - For subclasses, initialize subclass properties before calling
super.init.
Frequently asked questions about Swift stored property initialization
Why does Swift require all stored properties to be initialized?
Swift requires full initialization so that code cannot access an instance containing undefined stored-property values. Once initialization completes, every required property has a predictable value.
Do optional properties need to be assigned in init?
No. An optional property declared without an explicit value is initialized to nil. You may still assign a non-nil value in the initializer when one is available.
Does a property with a default value need to be initialized again?
No. Its declaration already provides an initial value. The initializer may replace that value when required, but it is not necessary for satisfying Swift’s initialization rule.
Why does the error remain when the property is assigned inside an if statement?
The assignment must occur on every possible path. Add an else branch, assign an initial value before the condition, or use a conditional expression that always produces a value.
Should I use an implicitly unwrapped optional to fix this initializer error?
Usually not. An implicitly unwrapped optional can cause a runtime failure when accessed while nil. Prefer a required initializer parameter, a meaningful default value, or a regular optional that is handled safely.
TutorialKart.com