Swift Known Unknowns: Entry point for an iOS App

Murali Krishnan
Product Ride
Published in
3 min readApr 19, 2020

For many iOS developers who started with swift, this might sound like a very easy question. One look at the newly created project code and you could easily see that the entry point for an iOS application should be AppDelegate.swift.

If we want to do any changes to the root view controller, we can go to AppDelegate.swift and change the application(_ application: , didFinishLaunchingWithOptions) and the job is done. So logically AppDelegate has to the entry point. But, is it true??

Let’s do a simple code change to test this out. Comment out the @UIApplicationMain line in AppDelegate and try running you project.

You should be getting the following error that entry point is undefined.

Interesting 🤔 The application here looks for the entry point main which the compiler is not able to find. It is true that developers who started with Objective C might be more familiar with the main.m file. To fix this error we can create a main.swift file and add the following code.

Manual main.swift file

What this code does is that it calls a C function UIApplicationMain() with parameters: the command line arguments, the main Application object for which we can override the UIApplication object for customization (if nil is passed it take the UIApplication class) and the object which will serve as the delegate to get the updates from UIApplication object.

The same main can be implemented in Objective-C as below which calls the same C function.

Objective-C main.m

For most of the projects we don’t even have to touch this function or add any customization in the main file. Apple decided to make this simple by creating an attribute called @UIApplicationMain which is place on the top of your project’s AppDelegate file to denote that this will be the delegate object for UIApplication.

Thus the entry point for an iOS app is hidden behind the single attribute. Don’t let the looks deceive you :)

--

--