This was answered in this old thread
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.swiftfile that calls the
UIApplicationMainfunction. This is unnecessary boilerplate, so Swift has a nice shortcut to avoid it.
What does the"@" at the beginning mean?
@character introduces a Swift attribute, which modifies the following class declaration.
This will provide mor details
used this as main.swift
import UIKit UIApplicationMain( CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(AppDelegate.self) )
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
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.
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.
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?