How to Create a TCA Feature Code Snippet in Xcode
Learn how to create a reusable code snippet in Xcode for quickly scaffolding TCA feature modules with placeholders and code completion.
Creating reusable code snippets in Xcode can significantly speed up your development workflow, especially when using a structured architecture like TCA (The Composable Architecture). In this post, youโll learn how to create a TCA Feature code snippet that you can reuse across modules.
๐งน Why Create a TCA Feature Snippet?
When developing with TCA, setting up a new feature often involves the same boilerplate: defining State, Action, Reducer, and handling bindings. Automating this setup with a code snippet improves consistency and saves time.
โ๏ธ How to Create the Snippet in Xcode
- Open or write the code you want to reuse. Use the following TCA boilerplate as your starting code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import ComposableArchitecture
import Foundation
@Reducer
public struct <#FeatureName#> {
@ObservableState
public struct State {
}
public enum Action: ViewAction, BindableAction {
case binding(BindingAction<State>)
case view(View)
public enum View {
}
}
public var body: some ReducerOf<Self> {
BindingReducer()
Reduce(core)
}
private func core(
_ state: inout State,
_ action: Action
) -> Effect<Action> {
.none
}
public init() {}
}
๐ The <#FeatureName#> is a placeholder that Xcode will let you tab through and replace easily.
Select all of the code in Xcode.
Right-click on the selection and choose
Create Code Snippet.In the snippet editor:
- Name it:
TCA Feature - Completion shortcut:
tcafeature - Platform:
All - Language:
Swift - Save the snippet.
- Name it:
โ๏ธ Why This Structure?
@ObservableState: Enables SwiftUI view bindings withBindableState.Action: ViewAction, BindableAction: Unifies bindings and user interactions.BindingReducer(): Handles.bindingautomatically.Reduce(core): Improves code completion and clarity.init(): Allows instantiation in previews or modules.
โก How to Use the Snippet
- Type
tcafeatureand hit Return. - Or open the Snippets Library with
Cmd+Shift+Land drag the snippet. - Use
Tabto jump through placeholders.
๐ Adding to VS Code or Cursor
To use the same snippet in VS Code:
- Open Command Palette (
Cmd+Shift+P) โConfigure User Snippets - Select
swift.jsonor create a global one - Add:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
{ "TCA Reducer Template": { "scope": "swift", "prefix": "tcareducer", "body": [ "import ComposableArchitecture", "import Foundation", "", "@Reducer", "public struct ${1:FeatureName} {", " @ObservableState", " public struct State {", " $2", " }", "", " public enum Action: ViewAction, BindableAction {", " case binding(BindingAction)", " case view(View)", "", " public enum View {", " $3", " }", " }", "", " public var body: some ReducerOf {", " BindingReducer()", " Reduce(core)", " }", "", " private func core(", " _ state: inout State,", " _ action: Action", " ) -> Effect {", " $4", " .none", " }", "", " public init() {}", "}" ], "description": "Creates a TCA (The Composable Architecture) Reducer template" } }
Then:
- Type
tcareducerin a Swift file - Press Tab to navigate placeholders
Now you can boost productivity in Xcode and VS Code or Cursor with one reusable snippet. Happy coding! ๐
โ Support My Work
If you found this post helpful and want to support more content like this, you can buy me a coffee!
Your support helps me continue creating useful articles and tips for fellow developers. Thank you! ๐