뷰모델 initializing 편하게 하기

RudinP·2025년 12월 3일

Study

목록 보기
386/388

공통 뷰 모델 이니셜라이저 구현

import Foundation
import RxSwift
import RxCocoa

class CommonViewModel: NSObject {
    //네비게이션 아이템 바인딩
    let title: Driver<String>
    
    //의존성을 쉽게 주입하기 위해 프로토콜로 설정
    let sceneCoordinator: SceneCoordinatorType
    let storage: MemoStorageType
    
    init(title: String, sceneCoordinator: SceneCoordinatorType, storage: MemoStorageType) {
        self.title = Observable.just(title).asDriver(onErrorJustReturn: "")
        self.sceneCoordinator = sceneCoordinator
        self.storage = storage
    }
}

사용예

다른 ViewModel에서 상속하여 사용한다.

profile
iOS 개발자가 되기 위한 스터디룸/스터디의 레퍼런스는 모두 kxcoding

0개의 댓글