9

Any one knows the launch sequence of a swift app?. As i can see there is no main function in a swift application. Then who is responsible for doing this

// Entry point of a normal iOS app
int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

Your application object and AppDelegate is getting created here. But for swift app i was wondering who is creating these.

If any one knows, share your thoughts.

Binarian
  • 12,296
  • 8
  • 53
  • 84
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110

2 Answers2

11

If you look at AppDelegate.Swift class, you will find the @UIApplicationMain statement written in the global area of the file. As @Daniel also mention in his answer.

Now I tried commenting that line and compiling the code, look what I got..

enter image description here

In very simple language, error say compiler trying to search implicit entry/start for main executable , but not getting.

it shows @UIApplicationMain statement in AppDelegate.Swift creates a implicit entry point for Swift program.

And I tried to write main.swift class explicitly...it works.

my main.swift looks like..

import Foundation
import UIKit

UIApplicationMain(C_ARGC, C_ARGV, NSStringFromClass(UIApplication), NSStringFromClass(AppDelegate))
Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
6

the swift Book says:

Code written at global scope is used as the entry point for the program, so you don’t need a main function.

If you have a look at your AppDelegate there is a marker @UIApplicationMain which is outside of any scope and considered as entry point.

dibi
  • 3,257
  • 4
  • 24
  • 31