Post

Replies

Boosts

Views

Activity

Reply to How to conditionally show a SwiftUI Scene on macOS?
@main struct OscillatingEmbersApp: App {     var model: AppModel = AppModel.restoreFromUserDefaults()     var body: some Scene {         WindowGroup {             computedView()         }     }     func computedView() -> some View {         model.increment()         model.saveToUserDefaults()         if model.isEven() {             return AnyView(EvenView())         } else {             return AnyView(OddView())         }     } } class AppModel: Codable {     static let StorageKey = "AppModel.StorageKey"     var count: Int = 0     init(count: Int) {         self.count = count     }     func increment() {         count += 1     }     func isEven() -> Bool  {         count % 2 == 0     }     static func restoreFromUserDefaults() -> AppModel {         let defaults = UserDefaults.standard         let count = defaults.integer(forKey: AppModel.StorageKey)         return AppModel(count: count)     }     func saveToUserDefaults() {         let defaults = UserDefaults.standard         defaults.set(self.count, forKey: AppModel.StorageKey)     } } So I tried this in Xcode 12 on an emulator iPhone 12 Pro Max running 14.0. Each time I launch the app, I get an odd then even screen on successive launches. So I would add something that saves if the user has done onboarding then return the correct view?
Dec ’20