O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required