在 Mac 上使用 SwiftUI App Lifecycle 时,@UIApplicationMain 只能用于 'class' 声明错误
@UIApplicationMain may only be used on 'class' declarations error when using SwiftUI App Lifecycle on Mac
如果您在 Xcode 12 中创建一个新的 SwiftUI 应用程序并使用 SwiftUI 应用程序生命周期,则会生成此结构...当您为 Mac 构建时会生成此错误。 Apple 已在其 Xcode 12 发行说明 (https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes)
中指出 @main
是一个问题
人们已经讨论了 AppDelegate
class.. 的解决方法,但它不适用于 SwiiftUI 应用程序生命周期(当您为 [= 创建新的 SwiftUI 应用程序项目时 select 40=])(
除了切换到 AppDelegate 和 SceneDelegate 之外,还有人对此有解决方法吗?
//@main
@UIApplicationMain // @UIApplicationMain may only be used on 'class' declarations
struct MyApp: App { // Cannot find type 'App' in scope
var body: some Scene { // Cannot find type 'Scene' in scope
WindowGroup {
ContentView()
}
}
}
编辑:
这是我在使用 @main
属性时遇到的错误:
'MyApp' is annotated with @main and must provide a main static function of type () -> Void or () throws -> Void.
我可以添加静态主要功能部分,但它仍然无法解决在范围内找不到应用程序或在范围内找不到场景的问题。
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
MainView()
}
}
static func main() -> Void {
}
}
编辑编辑:
请注意,这不能解决我的问题...它仍然抱怨 @main
属性
您需要使用 Xcode 12.2 和最新的 macOS 11 测试版
如果您在 Xcode 12 中创建一个新的 SwiftUI 应用程序并使用 SwiftUI 应用程序生命周期,则会生成此结构...当您为 Mac 构建时会生成此错误。 Apple 已在其 Xcode 12 发行说明 (https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes)
中指出@main
是一个问题
人们已经讨论了 AppDelegate
class.. 的解决方法,但它不适用于 SwiiftUI 应用程序生命周期(当您为 [= 创建新的 SwiftUI 应用程序项目时 select 40=])(
除了切换到 AppDelegate 和 SceneDelegate 之外,还有人对此有解决方法吗?
//@main
@UIApplicationMain // @UIApplicationMain may only be used on 'class' declarations
struct MyApp: App { // Cannot find type 'App' in scope
var body: some Scene { // Cannot find type 'Scene' in scope
WindowGroup {
ContentView()
}
}
}
编辑:
这是我在使用 @main
属性时遇到的错误:
'MyApp' is annotated with @main and must provide a main static function of type () -> Void or () throws -> Void.
我可以添加静态主要功能部分,但它仍然无法解决在范围内找不到应用程序或在范围内找不到场景的问题。
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
MainView()
}
}
static func main() -> Void {
}
}
编辑编辑:
请注意,这不能解决我的问题...它仍然抱怨 @main
属性
您需要使用 Xcode 12.2 和最新的 macOS 11 测试版