Dependency Injection Tutorial for iOS: Getting Started |

In this tutorial, you’ll learn about Dependency Injection for iOS, as you create the profile page of a social media app in SwiftUI.

This is a companion discussion topic for the original entry at

hi, I found this error " Unexpectedly found nil while unwrapping an Optional value: file Sociobox/ProfileContentProvider.swift, line 55
user: User = DIContainer.shared.resolve(type: User.self)!,

not sure why is user throwing this error?

Yes I got the same. It is because the Container is not properly initialised. Try to follow until she writes: “Build and run”, before the " Extending the Functionality" paragraph.
It works for me after I added the code in SceneDelegate! :slight_smile:

Thanks for sharing your knowledge. I was hoping the Tutorial could include some important cases that are not explained when trying to do dependency injection with SwiftUI. In particular when a View has @StateObject it seems SwiftUI doesn’t like us to use dependency injection. So if you could include how to add dependencies like @StateObject, @State and @Binding will be great.

ProfileView does not contain the same code as in does in the article under ’ Identifying the Issue’ section. Perhaps we’re suppose to add the if/else and remove the provider.friendsView, etc, but that’s not clear.