A dependency Injection solution for SwiftUI. Thank you Antoine van der Lee and Vincent Pradeilles for initially showcasing this idea.
private struct UserServiceKey: DependencyKey {
static var currentValue: UserService = MyUserService()
}
extension DependencyValues {
var userService: UserService {
get { Self[UserServiceKey.self] }
set { Self[UserServiceKey.self] = newValue }
}
}
@Dependency(\.userService) private var userService
DependencyValues[\.userService] = MockUserService()