19

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
  • 46,897
  • 22
  • 145
  • 209
Vlad
  • 5,727
  • 3
  • 38
  • 59

5 Answers5

38

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
  • 18,478
  • 8
  • 57
  • 72
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
  • 431
  • 5
  • 6
3

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
  • 139
  • 1
  • 9
1

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
  • 71
  • 7
0

In my case I had some code this file:

import SwiftUI

@main
struct FooWidgetExtension: WidgetBundle {
    var body: some Widget {
        SomeWidget()
    }
}

private struct SomeWidget: Widget {
    init() {}

    var body: some WidgetConfiguration {
        EmptyWidgetConfiguration()
    }
}

And the issue was actually you need to have an import WidgetKit as well. The main function must be defined as an extension in that module.

Keith Smiley
  • 61,481
  • 12
  • 97
  • 110