Like structs, classes are used to model related data under a common type. Classes differ from structs in a few very important ways, and this section will be careful to highlight those differences. You will use classes in MonsterTown to model various types of monsters that will be terrorizing your town.

A monster class

Now that you have a struct representing a town, it is time to make things a little more interesting. Your town is, unfortunately, infested with monsters. This is not good for property values.

Create a new Swift file called Monster. As before, click FileNewFile... or press Command-N. Select Source in the OS X section and the Swift File template.

This file will contain the definition for a Monster ...

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.