Structs

Structs (which is short for structures) are copied when they’re passed around. Classes are passed around by reference. This means that you will never have the same instance of a struct. Conversely, you can have multiple instances of the same class.

Here is what classes and structs have in common:

■ Both define properties to store values.

■ Both define methods to provide functionality.

■ Both provide subscripts to give access to their values.

■ Both provide initializers to allow you to set up their initial state.

■ Both can be extended to provide additional functionality beyond a default implementation. (This is different from inheritance.)

■ Both have the ability to conform to protocols (which you will learn about in Chapter 8).

Note ...

Get Learning Swift™ Programming 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.