2

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

cgold
  • 4,075
  • 1
  • 12
  • 13

2 Answers2

2

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 {...}
TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • here it was a function in the @main struct. The strange thing: it had worked for days, then suddenly didn't do anymore. – brainray Aug 17 '21 at 17:03
0

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)
        }
    }
}
novol
  • 832
  • 4
  • 20
  • Thanks, but I don't quite understand (and this could be my lack of background) how this is different from what I'm using (I just left out a bunch of code in the `...`)? For me it'd be most helpful to know what steps I can take to isolate the error. Maybe you can already tell and I didn't catch it - is it clear to you what exactly is causing this error? – cgold Jan 25 '21 at 03:11
  • unfortunately, I cannot say for sure, the solution to the problem is either to fix the code using the code I presented, or the structure is broken in your project, xcode 12.3 can also cause an error, I personally switched to 12.2 because in 12.3 there were many things that broke the integrity of my projects – novol Jan 25 '21 at 15:55