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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With