Declaration
@frozen @propertyWrapper struct EnvironmentObject<ObjectType> where ObjectType : Observable Object
what does that mean ?
my understanding is that variable wrapped @EnvironmentObject can be shared
across all views and update view when the variable is updated.
This is like the way React/Redux store doing.
final class UserData: ObservableObject {
@Published var showFavoritesOnly = false
@Published var landmarks = landmarkData
}
struct LandmarkList: View {
@EnvironmentObject var userData: UserData
var body: some View {
NavigationView{
List {
Toggle(isOn: $userData.showFavoritesOnly , label: {Text("Favorites only")})
ForEach(userData.landmarks) { landdata in
if (!self.userData.showFavoritesOnly || landdata.isFavorite) {
NavigationLink(destination: ContentView(landmark: landdata)) {
LandmarkRow(landmark: landdata)
}.navigationBarTitle(Text("Landmark"))
}
}
}
}
}
}
0 件のコメント:
コメントを投稿