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