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

What Should I Use?

Structures and classes are well suited for defining many custom types. Before Swift, structs were so distinct from classes in OS X and iOS development that the use-cases for both types were obvious. In Swift, however, the functionality added to structs makes their behavior more similar to that of classes. This similarity makes the decision of which to use somewhat more complicated.

But do not despair. There are important differences between structs and classes that give some guidance on which to use when. Strict rules are hard to define because there are many factors to consider, but here are some general guidelines.

  1. If you want a type to be passed by value, use a struct. Doing so will ensure that the type is ...

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