-1

I'm a beginner for swift. I knew that Swift is processed in order but program has a starting point and I think it is not Viewcontroller.swift in xcodeproj. Where is the starting point?

1 Answers1

4

All applications, whether it is Swift or Objetive C, starts with main class. But swift has auto implementation of main generated by @UIApplicationMain attributes. The simple implemetation of @UIApplicationMain would be like:

UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(AppDelegate))

which indicates to run the AppDelegate class.

Let's comment out @UIApplicationMain from AppDelegate and run. You will get build fail with two errors, like

Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation).

See, it does need a main as entry/start point.

Now create a main.swift file and write the following code and run again.

import UIKit
UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(AppDelegate))

It will compile successfully and will load the view which is exactly same as @UIApplicationMain does.

x4h1d
  • 6,042
  • 1
  • 31
  • 46