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.
-
If you want a type to be passed by value, use a struct. Doing so will ensure that the type is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access