Updated for Xcode 14.2
By default, all Swift structs get a synthesized memberwise initializer by default, which means that we automatically get an initializer that accepts values for each of the struct’s properties. This initializer makes structs easy to work with, but Swift does two further things that make it especially clever.
First, if any of your properties have default values, then they’ll be incorporated into the initializer as default parameter values. So, if I make a struct like this:
struct User {
var name: String
var yearsActive = 0
}
Then I can create it in either of these two ways:
struct Employee {
var name: String
var yearsActive = 0
}
let roslin = Employee(name: "Laura Roslin")
let adama = Employee(name: "William Adama", yearsActive: 45)
This makes them even easier to create, because you can fill in only the parts you need to.
The second clever thing Swift does is remove the memberwise initializer if you create an initializer of your own.
For example, if I had a custom initializer that created anonymous employees, it would look like this:
struct Employee {
var name: String
var yearsActive = 0
init() {
self.name = "Anonymous"
print("Creating an anonymous employee…")
}
}
With that in place, I could no longer rely on the memberwise initializer, so this would no longer be allowed:
let roslin = Employee(name: "Laura Roslin")
This isn’t an accident, but it’s a deliberate feature: we created our own initializer, and if Swift left its memberwise initializer in place then it might be missing important work we put into our own initializer.
So, as soon as you add a custom initializer for your struct, the default memberwise initializer goes away. If you want it to stay, move your custom initializer to an extension, like this:
struct Employee {
var name: String
var yearsActive = 0
}
extension Employee {
init() {
self.name = "Anonymous"
print("Creating an anonymous employee…")
}
}
// creating a named employee now works
let roslin = Employee(name: "Laura Roslin")
// as does creating an anonymous employee
let anon = Employee()
SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.