Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift] 클래스와 구조체를 사용하는 이유 #41

Closed
seungchan2 opened this issue May 17, 2022 · 0 comments
Closed

[Swift] 클래스와 구조체를 사용하는 이유 #41

seungchan2 opened this issue May 17, 2022 · 0 comments
Assignees
Labels

Comments

@seungchan2
Copy link
Owner

클래스와 구조체를 사용하는 이유

1. 사용하려는 목적의 설계

Data Transfer Object (DTO/VO): 관련 데이터를 한 곳에 담는 역할 ⭐️

Data Access Object (DAO): 데이터 처리 (비지니스 로직)

Helper Object: 유틸리티 (도움을 주는 기능/ 날짜, 시간, 통화 인코딩)

2. 애플이 미리 설계해 놓은 클래스 / 구조체들을 잘 사용하기 위함 (프레임워크)

그럼 언제 사용해야할까?

이 부분을 참고하셈

클래스와 구조체는 언제 사용해야할까?

사실 정답은 있는 게 아님

연관된 데이터를 단순히 캡슐화 하는 것이 목적이면 구조체 사용

캡슐화한 데이터를 참조하는 것보다 복사해서 사용하는 것이 효율적일 때 구조체 사용

구조체에 저장된 저장 속성들이 값 타입이며 참조하는 것보다 복사하는 것이 합당할 때 구조체 사용

struct AddPillInfo: Codable {
    let senderID: Int
    let senderName: String
    let receiverID: Int
    let receiverName, createdAt: String
    let sendPillInfo: [AddPillInfo]

    enum CodingKeys: String, CodingKey {
        case senderID = "senderId"
        case senderName
        case receiverID = "receiverId"
        case receiverName, createdAt, sendPillInfo
    }
}

데이터에서 상속의 구조가 필요하면 클래스 사용

해당 모델을 serialize 해서 전송하거나 파일을 저장할 때 클래스 사용

import UIKit

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        style()
        hierarchy()
        layout()
    }
    
    public func style() {
        navigationController?.navigationBar.isHidden = true
    }
    public func hierarchy() {}
    public func layout() {}
}
@seungchan2 seungchan2 self-assigned this May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant