Skip to main content

When would you use a struct versus a class?

· One min read
Ace the iOS Interview
Aryaman Sharda
Sources & Resources

Main Source: đź”— Ace the iOS Interview

Additional Sources:

Further Reading:

TL/DR

Typically, you’ll want to use astructif any of thefollowing conditions apply:

  • Use astructwhen encapsulating simple data types
  • When you need thread safety asstructsare passed-by-value
  • You want pass-by-value semantics
  • When the properties defined inside the entity are mostly value types
  • You don’t need inheritance
  • You don’t need mutability
  • When you want automatic memberwise initializers

Apple’s recommendation is to start with astructandtransition to aclassonly if you need inheritance or pass-by-reference semantics. However, if your entity is storing a lot of data then it may make sense to use aclassso you’re only incurringthe memory cost once.