Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 479
diegomontoyas Avatar asked Apr 02 '15 20:04

diegomontoyas


People also ask

What is @UIApplicationMain?

UIApplicationMain(_:_:_:_:)Creates the application object and the application delegate and sets up the event cycle. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.0+ tvOS 9.0+

What does the@ main attribute do?

Xcode 12 ships with Swift 5.3 which introduces the @main attribute as a general purpose way of marking the entry point of a Swift program. You use it in place of Apple specific attributes like @UIApplicationMain and @NSApplicationMain . If you're writing iOS or macOS apps you can probably just accept that and move on.

What is @main in Appdelegate?

UIApplicationMain first instantiates UIApplication and retains its instance to serve as the shared application instance ( UIApplication. shared ) and then instantiates the app delegate marked @Main as the application instance's delegate. The main method exists as a type method.

What is @main in Swift?

A Swift language feature for designating a type as the entry point for beginning program execution. Instead of writing top-level code, users can use the @main attribute on a single type. Libraries and frameworks can then provide custom entry-point behavior through protocols or class inheritance.


3 Answers

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.

like image 178
diegomontoyas Avatar answered Oct 20 '22 00:10

diegomontoyas


Same happen to me with a library that contains a main.swift ... Delete that one and problem solved.

like image 20
user1544073 Avatar answered Oct 20 '22 01:10

user1544073


Apple documentation, Swift / “Files and Initialization”:

earlier we said top-level code isn’t allowed in most of your app’s source files. The exception is a special file named “main.swift”, which behaves much like a playground file, but is built with your app’s source code. The “main.swift” file can contain top-level code, and the order-dependent rules apply as well. In effect, the first line of code to run in “main.swift” is implicitly defined as the main entrypoint for the program.

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”.

In Xcode, Mac templates default to including a “main.swift” file, but for iOS apps the default for new iOS project templates is to add @UIApplicationMain to a regular Swift file. This causes the compiler to synthesize a main entry point for your iOS app, and eliminates the need for a “main.swift” file.

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.

like image 12
Philippe-André Lorin Avatar answered Oct 19 '22 23:10

Philippe-André Lorin