Post

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.

How to Create a TCA Feature Code Snippet in Xcode

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

  1. 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.

  1. Select all of the code in Xcode.

  2. Right-click on the selection and choose Create Code Snippet.

  3. In the snippet editor:

    • Name it: TCA Feature
    • Completion shortcut: tcafeature
    • Platform: All
    • Language: Swift
    • Save the snippet.

⚙️ Why This Structure?

  • @ObservableState: Enables SwiftUI view bindings with BindableState.
  • Action: ViewAction, BindableAction: Unifies bindings and user interactions.
  • BindingReducer(): Handles .binding automatically.
  • Reduce(core): Improves code completion and clarity.
  • init(): Allows instantiation in previews or modules.

⚡ How to Use the Snippet

  • Type tcafeature and hit Return.
  • Or open the Snippets Library with Cmd+Shift+L and drag the snippet.
  • Use Tab to jump through placeholders.

📅 Adding to VS Code or Cursor

To use the same snippet in VS Code:

  1. Open Command Palette (Cmd+Shift+P) → Configure User Snippets
  2. Select swift.json or create a global one
  3. 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 tcareducer in 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! 🚀

This post is licensed under CC BY 4.0 by the author.