0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SwiftUI TCA Basic

Last updated at Posted at 2025-07-30

TCA Map

スクリーンショット 2025-07-30 20.28.08.png

Add swift composable architecture

https://github.com/pointfreeco/swift-composable-architecture"

スクリーンショット 2025-07-30 20.28.55.png

実装 - import

import ComposableArchitecture

実装 - code

struct State: Equatable {
    var counter = 0
} 

enum Action: Equatable {
    case increaseCounter
    case decreaseCounter
}

struct Enviroment {

}

let reducer = Reducer< State, Action, Enviroment> { state, action, enviroment in
    switch action {
    case .increaseCounter:
        state.counter += 1
        return Effect.none
    case .decreaseCounter:
        state.counter -= 1
        return Effect.none
    }
}

struct ContentView: View {
    let store: Store<State, Action>

    var body: some View {
        WithViewStore(self.store) { viewStore in
            HStack {
                Button {
                    viewStore.send(.decreaseCounter)
                } label: {
                    Text("-")
                        .padding(8)
                        .background(.red)
                        .foregoundColor(.white)
                        .cornerRadius(8)
                }
                .buttonStyle(.plain)

                Text(viewStore.counter.description)
                    .padding(10)

                Button {
                    viewStore.send(.increaseCounter)
                } label: {
                    Text("+")
                        .padding(8)
                        .background(.red)
                        .foregoundColor(.white)
                        .cornerRadius(8)
                }
            }
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(
            store: Store(
                initialState: State(),
                reducer: reducer,
                enviroment: Enviroment()
            )
        )
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?