swift - 在 Xcode 12 中使用 @main

标签 swift swiftui ios14 xcode12

我想在 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!")
      }
    }
  }
}
但它告诉我这个错误
Error Image

最佳答案

关注 @the.blaggy 答案,这是我设法在 iOS 13 上运行我的项目的方法:

  • 如果没有,请创建一个 SceneDelegate

  • SceneDelegate.swift
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            let contentView = ContentView()
    
            // Use a UIHostingController as window root view controller.
            if let windowScene = scene as? UIWindowScene {
                let window = UIWindow(windowScene: windowScene)
                window.rootViewController = UIHostingController(rootView: contentView)
                self.window = window
                window.makeKeyAndVisible()
            }
        }
    }
    
  • 打开 info.plist 作为源代码并添加这些行:

  • Info.plist
       <key>UIApplicationSceneManifest</key>
           <dict>
               <key>UIApplicationSupportsMultipleScenes</key>
               <false/>
               <key>UISceneConfigurations</key>
               <dict>
               <key>UIWindowSceneSessionRoleApplication</key>
               <array>
                   <dict>
                       <key>UISceneConfigurationName</key>
                       <string>Default Configuration</string>
                       <key>UISceneDelegateClassName</key>
                       <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                   </dict>
               </array>
           </dict>
       </dict>
    
  • 将此添加到您的 WeatherProApp.swift

  • WeatherProApp.swift
        @main
        struct WeatherProAppWrapper {
            static func main() {
                if #available(iOS 14.0, *) {
                    WeatherProApp.main()
                }
                else {
                    UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(SceneDelegate.self))
                }
            }
        }
    

    关于swift - 在 Xcode 12 中使用 @main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62935053/

    相关文章:

    Swift 5 - 传递给不带参数的调用的参数

    ios - 有没有办法解决iOS 14 Widget闪烁问题?

    ios - 清除 SwiftUI 中表单部分的背景?

    swift - 与 NavigationControllerDelegate 的冗余一致性

    swift 2 : How to downcast an Array of Objects

    ios - 如何使用 SiriKit 通过 Web 服务发送数据?

    ios - PHObject localIdentifier 可靠性

    list - 使可扩展列表的背景透明似乎是不可能的。在 StackOverflow 上测试了所有解决方案

    ios - SKAdNetwork-生成签名-如何确定使用哪个版本?

    ios - 单击按钮后立即执行 SwiftUI 按钮操作,而不是单击释放