Dependency Injection in SwiftUI

Dependency Injection in SwiftUI

There are different ways to do dependency injection in SwiftUI. In this post, we'll look at two of them:

[…]

Let's have a look at the two approaches to inject this dependency.

[…]

The views that need access to ReadingListController can get it via @EnvironmentObject; the others don't have to know about it.

[…]

We can call ViewModelFactory at the top level of our SwiftUI application, be it the App or UIWindowSceneDelegate implementation, to get the view models for the root views.

@EnvironmentObject trades runtime-safety for conciseness and is a more text-book SwiftUI approach, but it can crash your app.

[…]