What is the entry point of swift code execution?
Asked Answered
D

6

128

There is no main() method in swift. The program must start the execution from somewhere. So what is the entry point of swift code execution and how is it decided?

Danseur answered 8/6, 2014 at 11:30 Comment(0)
A
109

The entry point in a plain Swift module is the file in the module called main.swift. main.swift is the only file which is allowed to have expressions and statements at the top level (all other Swift files in the module can only contain declarations).

Cocoa Touch uses the @UIApplicationMain attribute on an implementation of UIApplicationDelegate instead of a main.swift file to mark the entry point. Cocoa used to use a minimal main.swift file which simply called NSApplicationMain, but as of Xcode 6.1 uses the @NSApplicationMain attribute on an implementation of NSApplicationDelegate.

Asphyxiate answered 8/6, 2014 at 11:34 Comment(5)
I created a new xcode project and I'm not able to find main.swift file anywhere. What is the location of this file?Danseur
In Cocoa and Cocoa Touch applications main.swift is in the Supporting Files group, and is simply configured to call NSApplicationMain.Asphyxiate
No its not there in Supporting files group. Its not anywhere in the project.Danseur
Sorry, I was wrong about Cocoa Touch but correct about Cocoa. See my updated answerAsphyxiate
Of course, even in Cocoa Touch you can create and use main.swift.Reactant
G
48

In the AppDelegate.swift file you can see @UIApplicationMain.
The AppDelegate is the initial entry file.

Basically: main.m and AppDelegate.m are kinda merged in Swift to just AppDelegate.swift

Githens answered 8/6, 2014 at 11:47 Comment(2)
@Asphyxiate He tagged iOS8, that goes for Cocoa-Touch, my answer is right.Githens
Nice, I was wondering how could I change main.m to swift and I didn't find a single thing.Zarf
C
35

You may want to read Files and Initialization

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. This allows the minimal Swift program to be a single line — as long as that line is in “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.

Alternatively, you can link in an implementation of main written in Objective-C, common when incrementally migrating projects from Objective-C to Swift.

Cataclysm answered 15/1, 2016 at 4:41 Comment(2)
I found this to be a way better and higher level explanation of how the entry point is set up in iOS.Blackstone
Nice answer, and good link. I then found a good place to put code that needs to run "early". Rather than use one of the app delegate methods, I simply override init on the app delegate, and do it there. I used it e.g. to set up the default logging level.Moonlighting
P
17

In Swift 5.3 there is a new @main attribute which lets you control where your entry point is in your project rather than just main.swift. There can only be one main entry and you can't have a main.swift file and a an attribute @main. See https://github.com/apple/swift-evolution/blob/master/proposals/0281-main-attribute.md for more details.

@main
struct App {
    static func main() {
        print("Starting.")
    }
}
Paragrapher answered 7/6, 2020 at 20:25 Comment(0)
D
3

In Swift apps there are attributes:

  • @UIApplicationMain (Cocoa Touch)
  • @NSApplicationMain (Cocoa)

that tell the swift compiler where is the entry point of the application.

What swift compiler does under the hood is that it creates a main function, which basically looks the same as in Objective-C apps and treats this method as the app's entry point (a first method that is called when the application process is started).

If you want to read more about what swift compiler does with Main attributes, how the OS knows where is the entry point of the application, I encourage you to read this article: Understanding iOS app entry point

Deepfry answered 4/5, 2020 at 10:48 Comment(0)
H
0

enter image description here

The entry point in a plain Swift module is the file in the module called.

Hut answered 20/11, 2022 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.