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. When you entered code like var myTown = Town(), that syntax called the empty initializer and set the new instance’s properties to ...

Get Swift Programming: The Big Nerd Ranch Guide 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.