在 Xcode 12 中使用 @main

人气:716 发布:2022-10-16 标签: swiftui swift xcode12 ios14

问题描述

我想在 iOS 13 及更高版本上运行此代码,我应该如何修复此错误?我想让这段代码也能在 iOS 13 上运行.

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

推荐答案

这可能取决于其他项目代码,但以下测试有效(Xcode 12b),因此可能会有所帮助.

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()
            }
        }
    }
}

513