Swift For Dummies
Book image
Explore Book Buy On Amazon

In the world of Swift, you may want to rethink how you use enumerations. Whereas in C, structures and enumerations are often used as simple types (that is, more or less as a way of saving keystrokes or organizing the code), structures, enumerations, and classes in Swift are all object‐oriented first‐class types.

Accordingly, then, in the world of Swift, it is common to refer to instances of structures and enumerations, as well as instances of classes.

When an instance of a structure or enumeration is passed from one code component to another, it is passed by value, whereas instances of classes are passed by reference. Putting it another way, instances of structures and enumerations are placed on the stack, whereas instances of classes are placed on the heap.

For developers interested in what this means to them and their app, it means that when you pass a unique copy of a structure or enumeration around in your app, that instance is actually moved around, and each function or other segment of code that touches it touches the instance that moves. When one segment of code makes a change to the instance data, it doesn’t affect other instances.

When you pass an instance of a class, that instance is shared among the sections of code to which it is passed (that’s because typically behind the scenes you only pass a pointer to the instance). If you make a change to a property of a class instance, however, everyone who is using that instance sees that change.

About This Article

This article is from the book:

About the book author:

Jesse Feiler is a developer, consultant, and author specializing in Apple technologies. He is the creator of Minutes Machine for iPad, the meeting management app, and Saranac River Trail and is heard regularly on WAMC Public Radio for the Northeast’s The Roundtable.

This article can be found in the category: