【问题标题】:Xcode 12 / iOS14 Widget "@main and must provide a main static function" errorXcode 12 / iOS14 Widget "@main and must provide a main static function" 错误
【发布时间】:2020-10-13 03:55:03
【问题描述】:

使用 Xcode 12 尝试为我的项目创建 Widget 应用扩展。

在创建新的 Widget 目标时,我收到以下错误:

'Widget' is annotated with @main and must provide a main static function of type () -> Void or () throws -> Void.

【问题讨论】:

    标签: swiftui widget ios14 xcode12 widgetkit


    【解决方案1】:

    您正在使用名称Widget,在SwiftUI 框架中已经有一个名为Widget 的协议。

    您应该使用其他名称,但如果您真的需要,请在开头添加模块名称,例如 SwiftUI.Widget

    @main
    struct Widget: SwiftUI.Widget {
        private let kind: String = "Widget"
    
        public var body: some WidgetConfiguration {
            IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider(), placeholder: PlaceholderView()) { entry in
                WidgetEntryView(entry: entry)
            }
            .configurationDisplayName("My Widget")
            .description("This is an example widget.")
        }
    }
    

    【讨论】:

    • 还有@main struct SwiftUIApp: SwiftUI.App
    【解决方案2】:

    对于任何希望在为 iOS14 项目打开 Mac 时修复 AppDelegate 问题的人 - 请咨询:iOS Xcode 12.0 Swift 5 'AppDelegate' is annotated with @main and must provide a main static function of type () -> Void or () throws -> Void

    解决方法是将“@main”更改为“@UIApplicationMain”

    【讨论】:

    • 对于 SwiftUI 项目:@UIApplicationMain may only be used on 'class' declarations
    • 所以,要解决这个问题,需要使用 Xcode 12.2 和 macOS 11 的新测试版
    【解决方案3】:

    我得到了以下代码的确切错误:

    @main
    struct NotesApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    

    问题是我将我的应用逻辑类命名为App

    【讨论】:

      【解决方案4】:

      这段代码我也遇到了同样的错误:

      import WidgetKit
      
      @main
      struct WidgetSale: Widget {
          let kind: String = "widgetSale"
      
          var body: some WidgetConfiguration {
              StaticConfiguration(kind: kind, provider: Provider()) { entry in
                  widgetSaleEntryView(entry: entry)
              }
              .configurationDisplayName("My Widget")
              .description("This is an example widget.")
          }
      }
      

      我通过在文件顶部导入 swiftUI 来解决它:

      import SwiftUI
      

      【讨论】:

        猜你喜欢
        • 2020-11-06
        • 1970-01-01
        • 1970-01-01
        • 2016-05-22
        • 2020-04-07
        • 2013-06-03
        • 2019-05-28
        • 1970-01-01
        • 2014-09-13
        相关资源
        最近更新 更多