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. For example, you will create a struct called Town in MonsterTown to model a town with a monster problem.

Making Town a struct encapsulates its data within a single type, and placing its definition in its own file provides a convenient location to find its implementation. In previous chapters, you modeled a town in a playground. Because the example was relatively small, this was not all that limiting. A playground is great for rapidly prototyping some code, but it does not really match the sort of projects that you will be working with in real app development. It ...

Get Swift Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.