【发布时间】:2020-11-06 03:26:18
【问题描述】:
我想在 iOS 13 及更高版本上运行此代码我应该如何修复此错误? 我想让这段代码也可以在 iOS 13 上运行。
@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!")
}
}
}
}
但它显示了这个错误
【问题讨论】:
-
那个错误信息很清楚。您不能在
@main 属性,只能在 iOS14+ 上使用。如果你想使用它,你要么需要将你的部署目标升级到 iOS 14,要么有 2 个单独的代码版本,一个是 if #available(iOS 14),一个是旧版本。
标签: swift swiftui ios14 xcode12