ios - 在 Mac 上使用 SwiftUI App Lifecycle 时,@UIApplicationMain 只能用于 'class' 声明错误

标签 ios swift xcode swiftui xcode12

如果您在 Xcode 12 中创建一个新的 SwiftUI 应用程序并使用 SwiftUI 应用程序生命周期,则会生成此结构...当您为 Mac 构建时,它会生成此错误。 Apple 已在其 Xcode 12 发行说明 (https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes)

中指出 @main 是一个问题

人们已经讨论了 AppDelegate 类的变通方法。但它不适用于 SwiiftUI 应用程序生命周期(您在为 iPhone 创建新的 SwiftUI 应用程序项目时选择)(iOS Xcode 12.0 Swift 5 'AppDelegate' is annotated with @main and must provide a main static function of type () -> Void or () throws -> Void

除了切换到 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”用@main 注释并且必须提供类型为 () -> Void 或 () throws -> Void 的主要静态函数。

我可以添加静态主要功能部分,但它仍然无法解决无法在范围内找到应用或范围内场景的问题。

 @main
 struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            MainView()
        }
    }

    static func main() -> Void {
    
    }
}

编辑编辑:

注意这并没有解决我的问题...它仍然提示 @main 属性 SwiftUI app life cycle iOS14 where to put AppDelegate code?

最佳答案

您需要使用 Xcode 12.2 和最新的 macOS 11 测试版

关于ios - 在 Mac 上使用 SwiftUI App Lifecycle 时,@UIApplicationMain 只能用于 'class' 声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64015183/

相关文章:

ios - 如何为某些用户隐藏栏按钮项目

ios - Firebase iOS swift : Updating TableView After Removing A Child

ios - 在 Swift 中读/写 JS 文件

swift - CloudRun 中的 Apple SwiftNIO - 这可能吗?

ios - 从 RTMP 流 ios 播放 aac 音频

.net - 让 .NET 链接按钮在 iOS Web 应用程序中工作

swift - UNTimeIntervalNotificationTrigger nextTriggerDate() 是否给出了错误的日期?

iOS 即时通讯服务 : Best way to to check for new messages?

ios - iPad 上的 iPhone 应用程序 : drawViewHierarchyInRect causes glitch

xcode - Storyboard内部不一致