Structs, classes, and other data structures
Ways of defining and handling structured data are an important part of any programming language that supports object-oriented programming. Swift has extended the patterns typical of Objective C, Java, and the like, to include structures that are passed by value, called structs, and those that are passed by reference, called classes (see Value and reference types in this chapter). There is also the much lighter-weight tuple, which offers a kind of bridge between data structures and collection types such as dictionaries.
As well as the fact that structs
are value types and classes
are reference types, there are a few other points of departure, but the two structures have a lot more similarities than they do differences.
Structs
A Swift struct
is basically a group of data, organized into properties, as well as methods that do something to or with those properties. There is more to come, but we'll start there for the sake of simplicity.
Let's set up a...