EnvironmentObject
is a way to share data between views in SwiftUI. To use it, you need to first create a reference to the object you want to share and then pass that reference to the views that need it.
Here is an example:
main.swift429 chars21 lines
In the example above, we first create a MyModel
object which conforms to ObservableObject
protocol. We then pass this object to ContentView
using the environmentObject
method. Finally, ContentView
can access the myModel
object by declaring @EnvironmentObject var myModel: MyModel
.
gistlibby LogSnag