1

I'm trying to add a simple SwiftUI view to an existing project, but @State changes are not propagated properly. The code works as expected in a new project, but doesn't work in the older project. The same thing happens with @ObservedObject.

Code:

struct ChannelAccountsView: View {

    @State private var showGreeting = true

    var body: some View {
        VStack {
            Toggle(isOn: $showGreeting) {
                Text("Show welcome message")
            }.padding()

            if showGreeting {
                Text("Hello World!")
            }
        }
    }
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            let contentView = ChannelAccountsView()
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
...
}

New project:

enter image description here

Old project:

enter image description here

Tried all of the obvious fixes:

  • Edited AppDelegate and added SceneDelegate
  • Edited Info.plist
  • Cleaned Project and the Derived data
  • Reset simulator data
  • Tested on multiple simulators/devices

Any idea what might be going on?

2
  • What do you mean by older project and how it differs from new project?
    – Asperi
    May 12, 2020 at 4:06
  • @Asperi older project -> project that existed for 2+ years and previously didn't use SwiftUI new project -> newly created project that had SwiftUI selected as the UI creation option
    – Ivan Rep
    May 12, 2020 at 8:27

2 Answers 2

3

I had a similar issue and the root cause was that my target had the Reflection Metadata Level set to None under Swift Compiler - General category inside Build Settings.

Changing it to All fixed it for me:

Reflection Metadata Level

0

Creating a new Target within the Project resolved the issues. Still not sure what the root cause is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.