Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Demystifying SwiftUI Previews

9b4d0f03e7d36e05eddbc6fbbf1b7fec?s=47 Marina
April 08, 2022

Demystifying SwiftUI Previews

Understand how SwiftUI previews work, why they pause, and how to fix it.

9b4d0f03e7d36e05eddbc6fbbf1b7fec?s=128

Marina

April 08, 2022
Tweet

More Decks by Marina

Other Decks in Technology

Transcript

  1. Demystifying SwiftUI previews Marina Gornostaeva | @hybridcattt

  2. Agenda ! Other use-cases " Preview lifecycle # The build

    process $ How live reloading works ⏸ Reduce previews pausing
  3. ! Other use-cases

  4. ! Other use-cases

  5. " Lifecycle of a SwiftUI preview • Build once •

    Live reload on changes • Pause if change is unsupported by live reloading • Repeat
  6. # Build process • For DEBUG • Detect by ENABLE_PREVIEWS

    build setting • Use ProcessInfo in runtime
  7. $ Dynamic replacement • Swift feature called @_dynamicReplacement • Pitched

    in 2018 • Supports changes in implementation of a function, computed variable, initializer or subscript struct MyStruct { dynamic func x() { print("x") } } extension MyStruct { @_dynamicReplacement(for: x()) func y() { print("y - replaced dynamically") } }
  8. $ Limitations of live reloading • It’s applied to everything

    in the current ile • Supported: changes to bodies of functions, properties, initializers • Not supported: • Editing comments • Signature changes • Adding/removing types, functions, properties • Initial value changes • Changes in other iles
  9. ⏸ Improving live reloading • Use computed vars • Check

    build scripts • Auto-resume previews
  10. Thanks & @hybridcattt https://hybridcattt.com/blog/ ixing-swiftui-previews