How should I reason when I have to choose between a class, struct and enum in Swift?

前端 未结 7 1793
感情败类
感情败类 2020-12-08 07:16

Since classes, structs and enums all has constructors, properties and computed properties, how should I reason when choosing between them?

相关标签:
7条回答
  • 2020-12-08 07:43

    For starters, Classes are pass-by-reference and Structs are pass-by-copy.

    enums are still a special type to specify, well, enumerations. They should be used just like before.

    In general, the choice of Class vs Structs shouldn't be much different than before. Classes are still fit for larger/complex objects and Structs are good for small, isolated model objects. The fact that structs have protocols and such now should just serve to simplify your code and make it more efficient.

    0 讨论(0)
提交回复
热议问题