14

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

pawello2222
  • 34,912
  • 15
  • 99
  • 151
Vlad
  • 5,453
  • 3
  • 35
  • 59

4 Answers4

29

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.")
    }
}
Bilal
  • 17,228
  • 8
  • 53
  • 69
7

For anyone looking to fix the AppDelegate issue when turning on Mac for an iOS14 project - please consult : iOS Xcode 12.0 Swift 5 'AppDelegate' is annotated with @main and must provide a main static function of type () -> Void or () throws -> Void

The fix is to change "@main" to "@UIApplicationMain"

odlh
  • 373
  • 4
  • 3
2

I got this exact error with the following code:

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

The problem was I named my app logic class App.

Alex Fine
  • 129
  • 7
0

I've got the same error with this code :

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.")
    }
}

I solve it by importing swiftUI at the top of the file :

import SwiftUI
Nicoz
  • 41
  • 6