r/simpleios • u/sohaeb • May 17 '16
[Question] Enum vs Struct
When I look at their syntax they look different, but when I try to think of some of their uses. I cant wrap my head around them. They somehow overlap according to my understanding.
The swift book by apple already explained the difference between Class and Struct. Can anyone tell explain when to use an enum vs a struct ?
2
Upvotes
View all comments
Show parent comment
1
u/schprockets May 23 '16
Which tells you the answer isn't clear cut. Please note, though, that the Swift manual isn't self-contradictory. He's saying it contradicts the advice given in the quoted WWDC talk.
Also, bear in mind you're not deciding how to write your entire app. This is a decision being made on an object-by-object basis, not a "should my app use only structs or only classes" decision.
In the end, it'll come down to your choice, based on a few things: Do you want value semantics or reference semantics when passing them around? Do you have a large number of properties, or a small number? Is it something that makes sense to be copyable? Does it need to interact with Objective-C? If your answers describe a struct, use a struct. If they don't, use a class.
In practice, because I interact with an existing Objective-C codebase a lot, my projects have tended to have far more classes than structs. Your mileage may vary.