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

Classes

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

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 the Swift File template from the Source section under macOS.

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

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