We finally have Hot Reload for Swift applications, thanks to the Inject tool written by Krzysztof Zabłocki, the great mind behind other amazing tools, like Sourcery.
In this article, we will create a simple project that uses the Inject library to make Hot Reload a reality in a Swift app.
You can download the Example project here.
The project
We’ll use a really simple view, that only contains a vertical stack view with 2 labels inside.
Setting up Inject
We need to do only 3
things to configure the library:
- Doenload InjectionIII from the Mac App Store.
- Open the InjectionIII app, select
Open Project
, and select the project Directory - Add the
Inject
library as SPM Package to your app:dependencies: [ .package( name: "Inject", url: "https://github.com/krzysztofzablocki/Inject.git", from: "1.1.1" ) ]
Making a ViewController hot-reloadable
Now we just need to wrap the VC we want to be hot-reloadable in a ViewControllerHost
:
import Inject
...
let vc = Inject.ViewControllerHost(HotReloadableViewController())
navigationController.setViewControllers([vc], animated: true)
...
That’s it, now if we run the project, we will be able to edit things on that view controller and see the changes instantly by hitting Cmd+S
.
If everything was set up correctly, you will see these messages in the console when reaching the injected view controller: