observableobject

SwiftUI - is it possible to get didSet to fire when changing a @Published struct?

人走茶凉 提交于 2021-02-18 11:23:05
问题 I have just updated to XCode 11.4 and some of my code has stopped working. I have some @Published struct variables in an ObservableObject . Previously, when I updated properties on the struct, the didSet method would fire on the published property, but that's not the case anymore. Is it possible that this behaviour has changed by design in the latest update to Swift? Here's a trivial example: import SwiftUI struct PaddingRect { var left: CGFloat = 20 var right: CGFloat = 20 } final class

Reference EnvironmentObject in ObservableObject

こ雲淡風輕ζ 提交于 2021-02-05 04:58:51
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

流过昼夜 提交于 2021-02-05 04:57:39
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

假装没事ソ 提交于 2021-02-05 04:57:38
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

谁都会走 提交于 2021-02-05 04:56:25
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

SwiftUI - ObservableObject created multiple times

爷,独闯天下 提交于 2020-12-28 20:39:39
问题 I have created an ObservableObject in a View. @ObservedObject var selectionModel = FilterSelectionModel() I put a breakpoint inside the FilterSelectionModel 's init function and it is called multiple times. Because this View is part of a NavigationLink , I understand that it gets created then and along with it, the selectionModel. When I navigate to the View, the selectionModel is created again. In this same View I have a "sub View" where I pass the selectionModel as an EnvironmentObject so

Assigning an ObservedState’s published value to a state

孤街浪徒 提交于 2020-06-28 06:41:26
问题 I have a state called time @State var time = 0 and an ObservedObject called timerWrapper @ObservedObject var timerWrapper = TimerWrapper() time can be updated from a child view and I want to be also able to update it using the timerWrapper (theObservedObject), if I use this: self.time = self.timerWrapper.remainingSeconds and do this: Text($time) The text doesn’t update. It only works if I do this: Text(self.timerWrapper.remainingSeconds) I know that’s because when remainingSeconds is

SwiftUI - propagating change notifications through nested reference types

五迷三道 提交于 2020-02-16 04:55:08
问题 I'd like to extend ObservableObject behavior in SwiftUI to nested classes, and I'm looking for the proper way to do it. It can be done "manually" with Combine, but I imagine there's a much cleaner way to do it using SwiftUI, and I'm hoping you can point me in the right direction. Here's what I mean… Below is a typical application of ObservableObject to make a View dynamically respond to changes to a reference type. Tapping the button toggles the showText value, which makes the text appear