슈뢰딩거의 고등어

모델 설계시 Structure, Class 중 어느 것을 사용해야할까 본문

14일만에 IOS 앱 끝내기 챌린지

모델 설계시 Structure, Class 중 어느 것을 사용해야할까

슈뢰딩거의 고등어 2023. 5. 21. 16:08

개인 프로젝트 도중, User 와 Post 에 사용할 모델을 만들어야 했다.

일단 Post 는 별 생각 없이 구조체로 만들었는데, 클래스로 만들어도 되지않나? 라는 생각이 문득 들어서

챗지피티에게 물어봤다.

 

The choice between using a structure (struct) or a class (class) depends on the specific requirements and characteristics of your application.
Here are some factors to consider:

  1. Mutability: Structures are value types, meaning they are typically immutable. When you create a new instance of a structure or assign it to a new variable, a copy is made. This can be beneficial in scenarios where you want to ensure immutability and avoid unexpected side effects. On the other hand, classes are reference types, and multiple references can point to the same instance. If you need mutability and shared references to the same instance, classes may be a better choice.
  2. Inheritance: Classes support inheritance, allowing you to create subclasses that inherit properties and behaviors from a base class. If you need to model an inheritance hierarchy or create specialized subclasses, classes are the appropriate choice. Structures, on the other hand, do not support inheritance.
  3. Performance: Structures are typically more lightweight and have better performance characteristics compared to classes. Since structures are copied when assigned or passed as function arguments, they can be more efficient in terms of memory usage. However, this performance advantage may vary depending on the specific use case and the size of the structure.

흠 그냥 구조체로 다 만들어도 될 것 같다.

따로 뭐 Inherit 가 필요한 성격이 아니기도 하고, 더 구조체가 가볍다고 하니...

 

각자의 프로젝트 성향에 맞게 하면 될듯한데, 단순 user 는 뭐 구조체정도로 뚝딱 만드는게 나을듯

Comments