Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

ios

swift

in my ios (swift) app, I have created main.swift in which I am setting a global variable against checking an NSDefault to check for ads removed.

then in each viewcontroller, i am first checking against that global variable and removing ads if appropriate before showing the view.

problem is that xcode does not like @UIApplicationMain in AppDelegate.swift because i have a main.swift. If I remove the line @UIApplicationMain the app crashes on launch.

Am I implementing main.swift wrong?

like image 809
TPCO Avatar asked Nov 06 '14 17:11

TPCO


People also ask

Is there a main function in Swift?

Swift doesn't have a main function; instead, it has a main file. When you run your project, the first line inside the main file that isn't a method or class declaration is the first one to be executed.

How do I create a Swift project in Xcode?

To create a new Swift package, open Xcode and select File > New > Swift Package. Choose a name and select a file location. Select “Create Git repository on my Mac” to put your package under version control. On completion, the Swift package opens in Xcode and looks similar to a standard Xcode project.

How do I run a Swift code in Xcode?

If you've installed the latest version of Xcode you should have a swift command available in your terminal, which launches a Swift REPL 1. You can run the Swift REPL from the command line and enter Swift code directly into it. Whenever you enter valid Swift code in the REPL, it will immediately compile and run it.

Can you make iOS apps with Swift?

Swift is a robust and intuitive programming language created by Apple for building apps for iOS, Mac, Apple TV, and Apple Watch. It's designed to give developers more freedom than ever. Swift is easy to use and open source, so anyone with an idea can create something incredible.


2 Answers

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]

like image 61
Ryan H. Avatar answered Sep 30 '22 10:09

Ryan H.


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.

like image 20
Hardy_Germany Avatar answered Sep 30 '22 08:09

Hardy_Germany