When should I use a class vs a struct?

Paul O'Neill
3 min readApr 19, 2022

--

This should be a question on every developer’s mind when writing code because it’s important. While classes and structs are similar in many ways, the details matter. There is one detail that is arguably the most important difference. That is that classes are reference types and structs are value types. What does that mean exactly?

Classes

Classes are reference types which means when a class object is passed around, what’s being passed around is a reference of the original space in memory for that object. That means also when I update the value of a passed object, the original object’s values will be updated because they are both referencing the same object. Below this code will illustrate this very process:

Structs

As mentioned above, structs are considered value types. This means that when structs are passed around, a copy of that object is made rather than a reference. So, if I pass a struct into the function, and then change the struct’s values, the original struct will not change. Below is an example of this:

When should I use a class vs a struct?

Based on the knowledge above, how can we decide? Well, how did the Swift Language team decide on their basic types?

Let’s first take a look at Int. It is declared as a struct. Why? The data it represents is the same no matter if it’s copied or not. If I have two Integers that are the same value, they should be equal to each other and that’s okay. We expect that. What about String? The same thing applies to a string. The value of two strings can equal the same and it would mean the same whether it’s copied or not. This applies to the types Double, Array and Dictionary as well.

What types do you think are declared as classes? UIView is a class. Why? If you think about having two UIView objects in memory, they represent two unique objects in the view hierarchy. So if we were to pass around these views, we need to keep a reference of them, because they are each unique. They are not interchangeable.

The answer to this question comes down to this: How is the data being used?

Other considerations

Some other things need to be considered when deciding between a class and a struct. Apple wrote an article with some general rules. I recommend you read it, but here are some bullet points summarizing the article:

  • Default to using a struct. Structs are more optimizing than classes as well as are thread-safe. Because of this, structs automatically eliminate a whole class of errors.
  • Use classes when the code needs to be used in Objective-C. Swift structs don’t exist in Objective-C.
  • Use classes when controlling identity. This is a common case with is local databases, file handling, or network connections.
  • Use structs when you don’t control identity. A common case for this is when modeling data from a server. If the object has an id, you don’t control how that id was generated.
  • While structs cannot inherit from other structs, they can inherit protocols. Use protocol inheritance when possible so that structs can be used.

In Summary

The differences between classes and structs might seem few, but those differences are quite expansive. Knowing the difference is vital to building and properly maintaining code.

--

--