Where is main entry of execution in "Swift"

Started by
1 comment, last by swiftcoder 9 years, 4 months ago

I'm baffled. I did C++ and Java. More on the Java side. Both languages's main entry of execution is the main method. But I do not see a main method in Swift.

I'm learning Game Programming using the Swift and I did some println statements just to see which one runs first.

import UIKit
import SpriteKit
 
class GameViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = GameScene(size: view.bounds.size)
        let skView = view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .ResizeFill
        skView.presentScene(scene)
        println("c")
    }
    
    override func prefersStatusBarHidden() -> Bool {
        println("a")
        return true
    }
}

import SpriteKit
 
class GameScene: SKScene {
    
    // 1
    let player = SKSpriteNode(imageNamed: "player")
    
    override func didMoveToView(view: SKView) {
        // 2
        backgroundColor = SKColor.whiteColor()
        // 3
        player.position = CGPoint(x: size.width * 0.1, y: size.height * 0.5)
        // 4
        addChild(player)
        println("b")
 
    }
}

Consoleoutput:

a

b

c

a

a

Based on the console output:

1) the override function prefersStatusBarHidden() in GameViewController.swift runs first

2) the override function didMoveToView in GameScene.swift runs second

3) the override function viewDidLoad in GameViewController.swift runs third

4) The fourth step consisting of step #1 running twice (based on the console output)

Is the main entry of execution in Swift actually what I pointed out above? Seems to be a break away from convention or tradition of executing a program.

Advertisement
http://stackoverflow.com/questions/24105690/what-is-the-entry-point-of-swift-code-execution

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Washu's link is correct, but keep in mind that this has very little to do with Swift, and everything to do with Cocoa/UIKit.

Apple's UI frameworks (whether for iOS or desktop Mac) are designed to take over the main program loop entirely. For example, if you create an new Objective-C application in XCode, you should end up with a 'main.c' file that contains something like the following:

#import <UIKit/UIKit.h>
 
int main(int argc, char *argv[])
{
    return UIApplicationMain(argc, (const char **)argv);
}

This is the boilerplate main entry point for a UIKit application, and it immediately delegates to the built-in NSApplicationMain() function. From the programmer's perspective, they are intended to ignore this, and place their initialisation code in [UIApplicationDelegate applicationWillFinishLaunching:withOptions:] instead.

The only real difference in Swift is that they've provided an @UIApplicationMain attribute which generates the boilerplate for you.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement