Swift Playgrounds on iPad Not Working with SwiftUI code as Expected

Viewed 372

The attached code works as expected on the Playgrounds simulator for the Mac but the States don't register a view update when the same code runs on an iPad. Does anyone know of a known issue/fix/workaround?

import SwiftUI
import PlaygroundSupport

struct MathGame: View
{
    @State var showAnswer = false
    @State var answer = ""
    @State var x = Int.random(in: 0...100)
    @State var y = Int.random(in: 0...100)
    var body: some View
    {
        VStack
        {
            Text(x.description)
            Text(y.description)
            if showAnswer
            {
                let z = x + y
                Text(z.description)
            } else
            {
                Text(" ")
            }

            
            Button("Next Question")
                {
                showAnswer = false
                x = Int.random(in: 0...100)
                y = Int.random(in: 0...100)
            }
            Spacer()
            Toggle(isOn: $showAnswer)
            {
                Text("Show Answer")
            } 
        }.padding().font(.system(size: 64))
    }
}

PlaygroundPage.current.setLiveView(MathGame())
1 Answers

I faced the same issue and I just found out that setting « Show results » to false was fixing it.

enter image description here

My iPad runs on iOS 14.6. I hope that the new Swift playgrounds with iOS 15 will fix this issue definitely.

Related