EnvironmentObject

Overview

Shares the data to the whoe project.

Subjects

Sharing data accross tabs

Hacking with Swift / 100 Days / Day-82 - Sharing data across tabs using @EnvironmentObject

Tags: #Tabs

Sample Codes

Using like a view modifier for simple use

struct ContentView: View {
    @StateObject private var user = User()

    var body: some View {
        VStack {
            EditView().environmentObject(user)
            DisplayView().environmentObject(user)
        }
    }
}
struct ContentView: View {
    @StateObject private var user = User()

    var body: some View {
        VStack {
            EditView()
            DisplayView()
        }
        .environmentObject(user). // put here for simple use
    }
}

...

Sources

Videos

Hacking with Swift / 100 Days / Day-79 - Reading custom values from the environment with @EnvironmentObject
Hacking with Swift / 100 Days / Day-82 - Sharing data across tabs using @EnvironmentObject

Articles / Documents

See also

Last updated