How to set an Environment Object in preview
You add it using .environmentObject(_:) modifier:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(YourObject())
}
}
This userData property gets its value automatically, as long as the environmentObject(_:)
modifier has been applied to a parent.
struct UserList: View {
@EnvironmentObject var userData: UserData
var body: some View {
NavigationView {
List {
Toggle(isOn: $userData.showFavoritesOnly) {
Text("Users Fav only")
}
ForEach(landmarkData) { landmark in
if !self.userData.showFavoritesOnly || landmark.isFavorite {
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
UserRow(landmark: landmark)
}
}
}
}
.navigationBarTitle(Text("Users"))
}
}
}
struct UserList_Previews: PreviewProvider {
static var previews: some View {
UserList()
.environmentObject(UserData())
}
}