SwiftUI 仅在预览中存在主要属性的问题

问题描述

我正在使用 App 方法运行 SwiftUI 应用程序 (Xcode 12.3):

@main
struct ThingsApp: App {
...
}

这在模拟器和我的设备上都按预期工作。但它在预览中不起作用:在那里,我收到 'main' attribute cannot be used in a module that contains top-level code 的错误。

一定是我的应用程序引起了它,因为如果我在 Xcode 中从头开始设置一个项目,这种方法是有效的。但我不太知道如何弄清楚究竟是什么导致了这种情况。更完整的跟踪如下:

'main' attribute cannot be used in a module that contains top-level code

----------------------------------------

CompileDylibError: Failed to build ThingsApp.swift

Compiling failed: 'main' attribute cannot be used in
a module that contains top-level code

/Users/cg/Library/Developer/Xcode/DerivedData/
Things-bkpepcogttixysdvumdszlfwxfix/Build/
Intermediates.noindex/
Previews/Things/Intermediates.noindex/Things.build/
Debug-iphonesimulator/Things.build/Objects-normal/x86_64/
ThingsApp.2.preview-thunk.swift:8:1: error: 'main' 
attribute cannot be used in a module that contains top-level code
@main extension ThingsApp {
^
/Users/cg/Library/Developer/Xcode/DerivedData/
Things-bkpepcogttixysdvumdszlfwxfix/Build/
Intermediates.noindex/
Previews/Things/Intermediates.noindex/
Things.build/
Debug-iphonesimulator/Things.build/
Objects-normal/x86_64/
ThingsApp.2.preview-thunk.swift:1:1: note: top-level code defined in this source file
@_private(sourceFile: "ThingsApp.swift") import Things

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)