在Xcode 12中使用@main [英] use @main in Xcode 12

查看:258
本文介绍了在Xcode 12中使用@main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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!")
      }
    }
  }
}

但是它显示了这个错误

推荐答案

这可能取决于其他项目代码,但是以下经过测试验证有效(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()
            }
        }
    }
}

这篇关于在Xcode 12中使用@main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆