All of a suden I got this error in my AppDelegate:
'UIApplicationMain' attribute cannot be used in a module that contains top-level code
in the line where it says: @UIApplicationMain
Now the project cannot build. I am using Swift and Xcode 6.2. I have tried deleting the derived data folder and even using the latest Xcode 6.3 beta, which was where I first created the project.
I don't know what's going on and I have searched everywhere with no luck. Thank you
Apple documentation, Swift / “Files and Initialization”:
Some file in your project is using top-level expressions, that is, statements outside of any function or block. This is only allowed in one file called “main.swift”.
If your project uses
@UIApplicationMain
, your project can’t have top-level expressions. (It also seems, according to my experience with XCode 8.2.1, that it can’t even have a file named “main.swift”, though I can’t find a reference that states so.)This explains why removing a rogue main.swift file from the project makes the error disappear.
Same happen to me with a library that contains a main.swift ... Delete that one and problem solved.
Ok, I finally solved it myself. It seems there was another file buried within a library I added called main.swift which was conflicting with my AppDelegate. I deleted it and the issue went away.