SwiftUI Tips

SwiftUI with Packages

I’ve been refactoring a codebase into reusable systems today.

When you have a View as part of a package, the following have to be public: the View, the Init, and body.

SwiftUI Environments api is iOS 17 or newer, doesn’t work inside of ViewModels, only inside of views.

This is an example of one way to use it.
@Environment(ViewModel.self) private var viewModel
@State private var viewModel = ViewModel()