structure of a Swift app - main.swift vs. AppDelegate

Recently I saw an example where someone removed the @UIApplication tag from AppDelegate.swift and created a main.swift. This didn‘t work for me and I also don‘t see the idea behind it.

Is there an overview of the structure of a Swift app in Xcode somewhere?

Accepted Reply

This was answered in this old thread


https://forums.developer.apple.com/thread/49061


In AppDelegate.swift file, I see a line of code at the very top, just below an import keyword. It says "@UIApplicationMain". What does that line of code do?

It indicates that this class should be treated as the app delegate for a UIKit based application. If you leave it out then you have to add a

main.swift
file that calls the
UIApplicationMain
function. This is unnecessary boilerplate, so Swift has a nice shortcut to avoid it.

What does the"@" at the beginning mean?

The

@
character introduces a Swift attribute, which modifies the following class declaration.


This will provide mor details

h ttps://medium.com/@Dougly/breaking-down-the-appdelegate-swift-3-258e48f907d6



Edited


used this as main.swift


import UIKit

UIApplicationMain(
    CommandLine.argc,
    CommandLine.unsafeArgv,
    nil,
    NSStringFromClass(AppDelegate.self)
)

Replies

This was answered in this old thread


https://forums.developer.apple.com/thread/49061


In AppDelegate.swift file, I see a line of code at the very top, just below an import keyword. It says "@UIApplicationMain". What does that line of code do?

It indicates that this class should be treated as the app delegate for a UIKit based application. If you leave it out then you have to add a

main.swift
file that calls the
UIApplicationMain
function. This is unnecessary boilerplate, so Swift has a nice shortcut to avoid it.

What does the"@" at the beginning mean?

The

@
character introduces a Swift attribute, which modifies the following class declaration.


This will provide mor details

h ttps://medium.com/@Dougly/breaking-down-the-appdelegate-swift-3-258e48f907d6



Edited


used this as main.swift


import UIKit

UIApplicationMain(
    CommandLine.argc,
    CommandLine.unsafeArgv,
    nil,
    NSStringFromClass(AppDelegate.self)
)

I created a sample project, and removed the line `@UIApplicationMain` in AppDelegate.swift,

and then added main.swift . The app runs fine without any problems.


If This didn‘t work for you, there may be something wrong in your main.swift. Please show your main.swift.

And please describe didn't work more precisely.

- It does not compile?

- Causes some runtime error?

- Or runs without error, but shows an unexpected odd behavior?

I tried out your example. Creating a main.swift by right click on the project folde and choosing "New file..." it generates a main.swift

starting with


import Foundation


Why does it not import UIKit?


(I'm developing an iOS app)



Your example then worked for me with the code example you gave. But I understand that it is probably not necessary to have an extra main.swift.


Thanks


--

Christoph

1) Any Swift file you create starts with the the Swift file template in the Xcode File Templates. There is nothing magical about it, it's just a text file that gets copied, some macros are replaced with stuff like the file name, copyright, etc., by Xcode, and placed in your project folder. You can get fancy and create a "UIKit Swift File" template if you want (lot's of references on the Internet, try searching "Xcode file templates"). Since Foundation is a common import for a large number of Swift files, it was added as a kind of "default." If you want to see what the template files look like, go to:


/Applications/Xcode.app/Contents/Developer/Xcode/Templates/File Templates (/Applications is normal installation for Xcode, your mileage may vary).


2). Although not common, it is sometimes convenient to call stuff before/after you call NS/UIApplicationMain. Being able to "build the main program from scratch" can be helpful.