SwiftUI Problem with main attribute only in Previews

2.1k Views Asked by At

I am running a SwiftUI app (Xcode 12.3) using the App approach:

@main
struct ThingsApp: App {
...
}

This works as expected both on simulators and my device. But it does not work in previews: There, I get the error of 'main' attribute cannot be used in a module that contains top-level code.

There must be something about my app that's causing it, since if I set up a project from scratch in Xcode, this approach works. But I don't quite know how to figure out what exactly would cause this. Fuller trace below:

'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

2

There are 2 best solutions below

1
On

For me it was a top level class type I created in the App extension:

@main
struct MyApp: App {
   var body: some Scene {}

   class AppDelegate: NSObject, UIApplicationDelegate {...}
}

Once I move it outside and did a clean build, previews worked again:

@main
struct MyApp: App {
   var body: some Scene {...}
}

private class AppDelegate: NSObject, UIApplicationDelegate {...}
2
On

if you are using environment your code should look like this

    @main
struct YourApp: App {
    @StateObject private var someData = SomeData()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(someData)
        }
    }
}