Hasty Briefsbeta

Bilingual

Hot reloading is better than SwiftUI previews

a day ago
  • Hot reloading provides faster iteration than Xcode Previews by dynamically loading code changes without restarting the app.
  • The technique uses dynamic libraries (.dylib) and the dlopen/dlsym functions to load and call new code at runtime.
  • Swift's name mangling is avoided by exporting functions with @_cdecl, and returning an NSHostingView to bridge SwiftUI and C.
  • A host app loads the library, uses NSViewRepresentable to embed the NSView, and periodically checks for file changes via a timer.
  • To force dlopen to load the updated library, the file is copied to a new name before each reload.
  • State persistence across reloads is achieved by storing the app state in the host and passing it to the library via createState and createStatefulView functions.
  • Incompatible state changes between versions may cause crashes; this can be mitigated by versioning the state and resetting when needed.