Posts

Post not yet marked as solved
0 Replies
215 Views
I'm pretty new to SwiftU and programming in general, I'm stuck and I could use some help. I'm trying to save a User to a MySQL DB using Vapor. The User struct has lots of properties which are set by Textfields, Pickers, and Toggles. The Textfields and Pickers work fine but the toggles do not.  I'll try to describe how I have this set up. In the view model for the create new user view I have confomed to ObservableObject and have a bunch of @Published properties - user name, password, etc and also a bunch of toggles to set various user permissions. As a test, I attached an observer to one property bound to a toggle:@Published var userDelete = false { 		didSet { 				print(userDelete) 		} } I can compile and setting the toggle back and forth shows the property being updated as expected in the console. Next I created a function to save the user data:func saveNewUser() { 				var newUser = User() newUser.userDelete = userDelete print("user delete: \(newUser.userDelete)") } This is where I see behavior I don't understand. The print call always returns false even when the observer returns true. I've removed all but one property for this example but Textfields and Pickers work as expected. I created a new, stripped down project just to see if that would help me narrow down the issue but that also works as expected so I'm kind of stuck, lacking the experience to know how to carry on hunting down what might be causing this. I'll add that code below. Any help appreciated, thanks! import SwiftUIstruct ContentView: View { @ObservedObject var viewModel = ViewModel.shared var body: some View { VStack { Toggle(isOn: $viewModel.testBinding) {. Text("Test Toggle") } .toggleStyle(SwitchToggleStyle()) Button("Save") { viewModel.testSave() } } .frame(width: 400, height: 300) .padding(20) } } class ViewModel: ObservableObject { static let shared = ViewModel() @Published var testBinding = false { didSet { print(testBinding) } } func testSave() { var model = Model() model.testBool = testBinding print(model.testBool) } } struct Model: Codable, Equatable, Hashable { var testBool = false }
Posted
by rougement.
Last updated
.