Post

Replies

Boosts

Views

Activity

How do I pass a value to a Class: ObservableObject a View?
Here is my ObservableObject Class I want to get a category based on which I will initialize the source with a trimmed list: //UsersViewModel.swift class UsersViewModel: ObservableObject { @Published var users: [User] @Published var category: String init(category: String) { self.category = category self.users = UserData().getUsers(byCategory: category) } } This is my View //UserListByCategory.swift import SwiftUI struct UserListByCategory: View { @EnvironmentObject var ud: UsersViewModel var body: some View { Text("Hello") } } struct UserListByCategory_Previews: PreviewProvider { static var previews: some View { UserListByCategory() .environmentObject(UsersViewModel(category: "Office")) } } I'm hardcoding the "Office" here, but is there a way to pass this to the Class?
0
0
692
Jan ’22