Struct Initialization
Structures can have both default and custom initializers. When working with structs, you will typically want to take advantage of the default initializer provided, but there are some circumstances in which you will want to customize the initialization process.
Default initializers for structs
Remember how you have been getting instances of your Town
type?
You gave the type’s stored properties default values.
What you did not know is that you were taking advantage of an empty initializer (an initializer without parameters) provided to you by the Swift compiler automatically.
If you could see its implementation, it would look like this:
init() { }
When you entered code like var myTown = Town()
, that syntax called the ...
Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.