Search code examples
swiftswiftuiios14xcode12

use @main in Xcode 12


I want to run this code on iOS 13 and above how should I fix this error? I want to make this code could run on iOS 13 too.

@available(iOS 14.0, *)
@main

struct WeatherProApp: App {
  @Environment(\.scenePhase) private var scenePhase
  @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

  
  var body: some Scene {
    WindowGroup{
      let fetcher = WeatherFetcher()
      let viewModel = WeeklyWeatherViewModel(weatherFethcer: fetcher)
      WeeklyWeatherView(viewModel: viewModel)
    }
    .onChange(of: scenePhase) { (newScenePhase) in
      switch newScenePhase {
      case .active:
        print("scene is now active!")
      case .inactive:
        print("scene is now inactive!")
      case .background:
        print("scene is now in the background!")
      @unknown default:
        print("Apple must have added something new!")
      }
    }
  }
}

but it shows me this error

Error Image


Solution

  • This might depend on other project code, but the following tested as works (Xcode 12b), so might be helpful.

    The idea is to hide one wrapper inside another structure with availability checker:

    @available(iOS 14.0, macOS 10.16, *)
    struct Testing_SwiftUI2AppHolder {
        @main
        struct Testing_SwiftUI2App: App {
    
            var body: some Scene {
                WindowGroup {
                    ContentView()
                }
            }
        }
    }