Let's think about it this way for a second
Dependency Injection (DI) is a pattern where a class/View doesn't create its own dependencies but instead receives them from the outside — this improves testability/maintainability. SwiftUI doesn't need a third-party DI library like Hilt (from the Android tutorial) — it comes with a built-in `Environment` mechanism (`.environmentObject()`, `@EnvironmentObject`). If you inject a dependency at the root View with `.environmentObject(weatherApi)`, any child View (no matter how deeply nested) can grab it with `@EnvironmentObject var weatherApi: WeatherApi`.
Let's connect this to a real-world scenario
If you inject at the root with `ContentView().environmentObject(WeatherApi())` — no matter how deep the nested View is (`WeatherScreen` → `WeatherDetailCard` → `TemperatureLabel`), it can grab it with `@EnvironmentObject var weatherApi: WeatherApi` — no need to manually pass it down parameter by parameter (`WeatherScreen(api:) → WeatherDetailCard(api:) → ...`).
Let's look at it together
// Root
@main
struct MyApp: App {
@StateObject private var weatherApi = WeatherApiClient()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(weatherApi)
}
}
}
// Deeply nested child — no manual parameter passing needed
struct TemperatureLabel: View {
@EnvironmentObject var weatherApi: WeatherApiClient
var body: some View {
Text("Connected: \(weatherApi.isReady)")
}
}You'll see that TemperatureLabel (deeply nested) can access the weatherApi instance without needing it manually passed down as a parameter.5-minute try-it
Inject a `WeatherApiClient` (a simple ObservableObject) at the root using `.environmentObject()`, then grab it with `@EnvironmentObject` two or three nesting levels down.
A quick heads-up
If a dependency for `@EnvironmentObject` isn't injected at the root, you won't get a compile-time error — it crashes at runtime instead (you also need to inject it the same way in SwiftUI Preview) — the good news is you can catch this kind of error early from the Preview panel, before your production build.