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
Hi,
It looks like you are passing the return Value from the GetValue() function into the Text Element.
Try this:
Code Block swift
 Text(sharedObj.CustomString)
        Button(action: {
            sharedObj.Update(input: "NewValue")
        }){
            Text("Update")
        }

When you call the update function the Custom String is automatically updated in the Text Element.

Take care,
David

Thanks for the reply. The reason I used the GetValue is to be able to see if it was getting called by adding a print statement.
I tried changing the code to directly use the CustomString as you suggested as well, but it did not work.

Have you tried the code on Playgrounds on IPad? I am suspecting that it might be an issue only on iPad.

Also how can we escalate to this to Apple Support?


Hi,
Well if the code snipped I supplied didn't work it is probably a bug with Swift Playgrounds. I tried it in a Sample Project in Xcode and it worked just fine. I you experience this only in Swift Playgrounds and only on iPad you could file a bug report here: https://feedbackassistant.apple.com

Take care,
David
Thanks! I have submitted a feedback. Will update the thread once I get reply.

I have submitted a feedback.

What was the bug number?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
This is the first time I have submitted feedback so I am guessing the feedback number is what you are referring as bug number. In my case its FB9000387.
No reply on the feedback submitted. Is this expected to not hear back on the feedback submitted for couple of months?
XCode + SwiftUI+ EnvironmentObject not working on iPad
 
 
Q