How to implement main.swift in Xcode 6 iOS app?

Your main.swift file will need to look something like this:

import Foundation
import UIKit

// Your initialization code here

UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(AppDelegate))

UIApplicationMain will start the event loop and prevent the app from exiting.

C_ARGC and C_ARGV are Swift vars that represent the C parameters that are passed in through main, namely int argc and char *argv[].

UPDATE 2016-01-02: C_ARGC and C_ARGV have been replaced by Process.argc and Process.unsafeArgv respectively. [source]


as of the very good answer to the question How do I access program arguments in Swift? from Darrarski...

The Swift 3 syntax would be:

//
//  main.swift
//

import Foundation
import UIKit

// very first statement after load.. the current time
let WaysStartTime = CFAbsoluteTimeGetCurrent()

// build the parameters for the call to UIApplicationMain()
let argc = CommandLine.argc
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc))

// start the main loop
UIApplicationMain(argc, argv, nil, NSStringFromClass(AppDelegate.self))

and don't forget to remove the "@UIApplicationMain" from AppDelegate.swift as it would be redundant and causing a compiler error.


Thats how the main.swift should look in Swift 5

Hardy_Germany's answer gives warning in Xcode 10.2.

//
//  main.swift
//  TopLevelCode
//
//  Created by Stoyan Stoyanov on 04/04/2019.
//  Copyright © 2019 Stoyan Stoyanov. All rights reserved.
//

import UIKit
import Foundation

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

also again don't forget to remove the @UIApplicationMain tag from your AppDelegate read more about it here

Tags:

Ios

Xcode

Swift