swiftuiwidgetios14xcode12widgetkit

Xcode 12 / iOS14 Widget "@main and must provide a main static function" error


Using Xcode 12 trying to create Widget app extension to my project.

Upon creating new Widget target I am getting the following error:

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

enter image description here


Solution

  • You are using name Widget, there is already a protocol called Widget in SwiftUI framework.

    You should use some other name but if you really want to, add module name at start like 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.")
        }
    }