Hot Module Reloading can save you hours of development each day. It works by only compiling parts of your application that have changed instead of having to recompile the entire application each time something changes.
Posted By Adam Bulmer In Swift,XCode
As a Web Engineer, I have been taking Hot Module Reloading for granted. It's something I don't even think about anymore. It's just part of my development workflow because most web frameworks support Hot Module Reloading by default.
Now in 2022 Hot Module Reloading is coming to Swift and it's really easy to set up.
To enable Hot Module Reloading requires the use of InjectionForXcode and a micro-library called Inject.
Inject
is a library created by Krzysztof Zabłocki and is universal across UIKit
, AppKit
and SwiftUI
.
Inject
is available as an SPM dependency. https://github.com/krzysztofzablocki/Inject.git
-Xlinker -interposable
to Other Linker Flags
within all targets in your project for Debug
configuration./Applications
folder./Applications
folder.InjectionForXcode
.Open project
.Add import Inject
to the top of the files you wish to Hot Reload.
At this point, you might be thinking we are modifying production code. The great thing about Inject
is that in a production build, the code compiles to NO-OP.
.enableInjection()
at the end of your body definition.@ObserveInjection var inject
to your view structimport Inject
import SwiftUI
struct HomeView: View {
@ObservedObject private var inject = Inject.observer
var body: some View {
ContentView()
.enableInjection()
}
}
Inject
provides two Host
helpers that aid in cleaning up the state between injection phases.
Inject.ViewControllerHost
.Inject.ViewHost
.In both cases, we don’t modify the class we want to be injecting but instead, want to modify the parent class.
let body = Inject.ViewHost(
YourInjectedViewController(your: arguments)
)
I can't believe it's 2022 and we are only now getting a good developer experience for Hot Module Reloading on Apple Platforms.
If you want to create your first native app or have just begun your native app development journey, be sure to sign up to the newsletter. No Spam