Post

Replies

Boosts

Views

Activity

XCode + SwiftUI+ EnvironmentObject not working on iPad
I am new to swift and SwiftUI and I am working with Swift Playgrounds V3.4 using the XCode playgrounds (swift 5.3 edition) and trying to make a simple SwiftUI code that updates text in a view however the text field in the view is not refreshing at all and only gets called during the initialize even after using the ObservableObject and @Published property Here is the full code. Can someone please help me figure out what is wrong with this code or is this not expected to work on iPad? import SwiftUI import PlaygroundSupport class SharedObject: ObservableObject {     @Published var CustomString = "Default Value"     func Update(input: String){         self.CustomString = input          print("Updated value : \(self.CustomString)")     }          func GetValue() -> String {         print("Returning value: \(self.CustomString)")         return self.CustomString     } } struct ContentView: View {     @EnvironmentObject var sharedObj: SharedObject     var body: some View {         // your code here         Text(sharedObj.GetValue())         Button(action: {             sharedObj.Update(input: "NewValue")         }){             Text("Update")         }     } } var shared = SharedObject() PlaygroundPage.current.needsIndefiniteExecution = true  PlaygroundPage.current.setLiveView(ContentView().environmentObject(shared)) ———- the debug output I get is as follows (this is when trying to press the button multiple times) Returning value: Default Value Returning value: Default Value Updated value : NewValue Updated value : NewValue
7
0
1.2k
Feb ’21