Value vs Reference types (struct/enum vs class)

Apple recommends that developers should prioritize using structs over classes because of the benefits structs provide like..value semantics.

Value semantics means that when you assign or pass a value (like a struct) to another variable or function, you're working with an independent copy of that value rather than a reference to the original.

Apple also states that using structs is safer, more predictable code through automatic copying, reduced side effects, and improved thread safety. While structs offer benefits like value semantics and thread safety, they aren’t always the best choice for every situation.

For one, Structs copy on asaignment, which means that you can not share mutable states easily. If multiple parts of your app need to reference and update the same instance, classes - with their reference semantics are a better fit.

When we talk about large data structures or those frequently mutated (In this context, “mutated” means that the value of an instance—its internal state or properties—is changed), the cost of copying a struct can add up. Classes avoid this by passing around references rather than copying entire instances.

Next
Next

Blog Post Title Two