Hot-Reloading — Swift and SwiftUI

Vikash Anand
4 min readApr 18, 2022

--

This article is about Hot-Reloading and how we can use it to speed — up our application development workflow.

Before that lets understand, how Hot-Reloading speeds up the workflow ?

App development is an iterative process where we do code changes and then to check the outcome, we have to compile, build, run and navigate / re-navigate to the screen where we were previously in the app and then re-produce the required data to show in the app.

This whole cycle(compile, build, run, re-navigate and re-produce the required data) takes a lot of time.

Hot-reloading solves this problem. It’s about getting rid of the re-compiling of the app as much as possible. You just have to save the changes and the changes will be automatically reflected in the already running application.

Hot reloading is a technique allowing you to get rid of compiling your whole application and avoiding deploy/restart cycles as much as possible, all while allowing you to edit your running application code and see changes reflected as close as possible to real-time.

Sourcehttps://merowing.info/2022/04/hot-reloading-in-swift/

See a demo video here https://github.com/krzysztofzablocki/Inject

Hot-Reloading works for following apple ecosystem’s UI frameworks

  1. UIKit / AppKit
  2. SwiftUI

Now lets see the steps required to integrate Hot-reloading in develpoment workflow

Add Inject SPM dependency

  • Add “-Xlinker -interposable” (without the double quotes) to the “Other Linker Flags” of all targets in your project for the Debug configuration.

Usage with UIkit / AppKit

  • Import Inject
  • Wrap the view controller / inside the ViewControllerHost / ViewHost

// WRONG

let viewController = YourViewController()

rootViewController.pushViewController(Inject.ViewControllerHost(viewController), animated: true)

// CORRECT

let viewController = Inject.ViewControllerHost(YourViewController())

rootViewController.pushViewController(viewController, animated: true)

Source — https://merowing.info/2022/04/hot-reloading-in-swift/

Usage in SwiftUI

  • Add @ObservedObject private var iO = Inject.observer variable
  • call .enableInjection() at the end of your body definition

Installation of InjectionIII app

You can get the latest release of the InjectIII from here- https://github.com/johnno1962/InjectionIII/releases

  • A zip file will be downloaded, un-zip it and then place it under to “/Applications” folder.
  • Also, make sure that Xcode is also present at its default location i.e in “/Applications/Xcode.app”.
  • Run the “Injection” app and then select open project / open recent from its menu and pick the right workspace file you are using.

Note — After running the app, a needle / Injection icon will appear in the status bar.

  • Run you application and after your application starts running in the simulator, you will be able to see the “InjectionIII connected” log in the Xcode’s console and the Injection app icon in the status bar will turn orange now.
  • Now go ahead and do your code changes and save it (cmd + s). You should be able to see the outcome of you code changes in the simulator without stopping and re-running the app.

Conclusion

That’s it for now, thanks for reading. Also if you really find this helpful then please leave some applause to this article 😀😀😀.

Resources

‎Swift Secrets ‎Computers & Internet · 2021 books.apple.com

Github Repositories Urls

--

--

Vikash Anand
Vikash Anand

No responses yet