개발/Swift

TCA binding 액션 내부 타입 바인딩 @CasePathable

덤벨로퍼 2025. 3. 25. 17:07

Binding

보통 아래와같은 방식으로 바인딩을 만들어 노출 비노출 처리함

→ Binding init 보다 더 짧고 간결하게 구현히 가능

$store.presentActionSheet.sending(\\.presentActionSheet),

근데 만약 Action 타입이 내부 세부 타입으로 나누어져있을 경우에 문제가 생김

enum Action {

 enum CommunityPostReportAction {
      case setActiveAlert(CustomAlertPopupType?)
      case showReportedPostGuide(Bool)
  }

  case report(CommunityPostReportAction)

}

이런경우 위와 같은 방식으로 바인딩 할수가 없음

$store.showReportedPostGuide.sending(\\CommunityPostDetailReducer.Action.CommunityPostReportAction.showReportedPostGuide
$store.showReportedPostGuide.sending(\\.report(.showReportedPostGuide))

Cannot convert value of type 'KeyPath<CommunityPostDetailReducer.Action.CommunityPostReportAction, (Bool) -> CommunityPostDetailReducer.Action.CommunityPostReportAction>' to expected argument type 'KeyPath<Case<CommunityPostDetailReducer.Action>, Case<Bool>>'

액션 타입을 CommunityPostDetailReducer.Action 과 맞추어야함

해결

@CasePathable 을 활용하면 dot binding 이 가능해짐

 enum Action {

 @CasePathable // 매크로 추가
 enum CommunityPostReportAction {
      case setActiveAlert(CustomAlertPopupType?)
      case showReportedPostGuide(Bool)
  }

  case report(CommunityPostReportAction)

}

그러면 이런식으로 접근이 가능함

 $store.showReportSuccess.sending(\\.reportAction.showReportSuccess)