Structures

A struct is a type that groups a set of related chunks of data together in memory. You use structs when you would like to group data together under a common type. You are going to create a struct called Town to model a town with a monster problem, along with its size, population, and region.

In previous chapters, you modeled a town in a playground. Because the example was relatively small, this was not all that limiting. Playgrounds use a streamlined approach to code management to facilitate quick prototyping of code, but app development requires a more robust set of tools for code organization. It is better to encapsulate the definition of the town within its own type, in its own file.

Add a new file to your project using File ...

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.