SwitchStore was a SwiftUI view used to switch over enum state and render different views for each case. Modern TCA allows you to use Swift’s native switch statement directly.
Declaration
Overview
SwitchStore was designed to work with enum-based state to show different views depending on which case is active. It worked in conjunction with CaseLet to extract associated values and scope the store.
Legacy Usage (Deprecated)
Modern Approach (Recommended)
With@ObservableState, use Swift’s native switch statement:
Parameters
Legacy SwitchStore Parameters
store: The store containing enum statecontent: A view builder that switches over the state and returns appropriate views
Migration Guide
Step 1: Add @ObservableState
Ensure your state enum has the@ObservableState macro:
Step 2: Replace SwitchStore with switch
ReplaceSwitchStore with a native Swift switch:
Step 3: Remove CaseLet
ReplaceCaseLet with optional binding and store.scope:
Working with Optional State
For optional enum cases, combineswitch with if let:
Benefits of Migration
- Native Swift: Use familiar
switchsyntax instead of custom views - Better Performance: Less view overhead
- Type Safety: Compiler-enforced exhaustiveness checking
- Simpler Code: Fewer custom types to learn
- Better Debugging: Standard Swift control flow
See Also
@ObservableState: Makes state observable using Swift’s Observation frameworkStore.scope: Transforms a store to work with child state and actionsifCaseLet: Reducer operator for handling enum cases- Migration Guide