ivme
ivme

Reputation: 558

How does xCode decide in which file to begin code execution?

I'm using xCode 6 to write a simple game in Swift. I have a variety of files in the project. How does xCode know where to begin when executing the code? It seems to me that it automatically starts with the file that, following various tutorials, I have called "GameViewController.swift." But how does it decide to use this file first? Does it scan the source files for a UIViewController object and start there?

Upvotes: 0

Views: 1146

Answers (3)

Dave Wood
Dave Wood

Reputation: 13333

For iOS, Swift looks for a class with the @UIApplicationMain attribute, and uses that as the Application's delegate.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    ...
}

Using that attribute is equivalent to calling the UIApplicationMain function and passing the class’s name as the name of the delegate class. That in turn will handle loading your default Storyboard and load its initial view controller.

If no @UIApplicationMain is found, Swift looks for a file called main.swift and for a function called main inside of that and calls it.

Upvotes: 2

carlodurso
carlodurso

Reputation: 2894

To give you an idea of Apple architecture here's an example of a Cocoa app which applies to iOS Objective-C as well:

enter image description here

Every application starts at main but the developer entry point is usually AppDelegate. This applies to Swift as well, even if main has disappeared from the application lifecycle. Bear in mind, that main existed just to launch the app or few minor tweaks.

enter image description here

If you're using Storyboards, you'll be able to set the starting screen of your app from there, conversely if everything is done in code AppDelegate will be responsible of it at this point:

func application(application: UIApplication, 
     didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

Upvotes: 0

vikingosegundo
vikingosegundo

Reputation: 52237

if you are using storyboards, you have a setting called "Main Interface" on your target. This offers a dropdown list where you can select the storyboard file. i.e. Main.storyboard. This setting will be written to the Info.plist file that will be loaded at start time.

In this file a view controller is set as the initial view controller and will be loaded first.

Upvotes: 0

Related Questions