HotSwiftUI

repository·main·Indexed 41 days ago

https://github.com/johnno1962/hotswiftui

A utility library for enabling live code updates in SwiftUI applications. It provides the @ObserveInjection property wrapper and the .enableInjection() modifier to trigger view redraws when using tools like HotReloading or InjectionIII.

Tokens
490
Snippets
2
Records
3
Agent score
50%

What's inside HotSwiftUI

  1. What is @ObserveInjection and how does it work?

    main
    The @ObserveInjection property wrapper is used within SwiftUI View structs to trigger a redraw when code is injected. It works by observing the global injectionObserver, which contains an @Published injection counter. When the counter changes due to a code injection event, any view observing this property will be forced to update by SwiftUI's standard observation mechanism.
  2. Install and setup HotSwiftUI

    main

    To use HotSwiftUI for live code updates, add the repository to your project and include the following line in a source file to export the module throughout your application:

    @_exported import HotSwiftUI

    This makes the .enableInjection() method on SwiftUI.View and the global injectionObserver variable available app-wide.

  3. How to enable live injection in SwiftUI Views

    main

    To ensure your SwiftUI views refresh when code is injected via HotReloading or InjectionIII, you must perform two steps in every View struct you wish to update:

    1. Add the @ObserveInjection var redraw property wrapper to the struct.
    2. Call the .enableInjection() modifier on the view's body.

    Note: In "Release" builds, these functions compile to a null operation and do not affect production performance. You can use the "Prepare Project" menu item in InjectionIII or HotReloading to automate these changes across your project.

    struct MyView: View {
        // 1. Add the property wrapper
        @ObserveInjection var redraw
    
        var body: some View {
            // Your SwiftUI code...
            Text("Hello World")
                // 2. Use the enableInjection modifier
                .enableInjection()
        }
    }