2014-11-06 74 views
4

在我的ios(swift)应用程序中,我创建了main.swift,其中我设置了一个全局变量来检查NSDefault以检查删除的广告。如何在Xcode 6 iOS应用程序中实现main.swift?

然后在每个视图控制器中,我首先检查该全局变量,并在显示视图之前删除适当的广告。

问题是xcode不喜欢AppDelegate.swift中的@UIApplicationMain,因为我有一个main.swift。如果我删除了@UIApplicationMain这一行,则应用程序在启动时崩溃。

我在执行main.swift错误吗?

+0

请发布您的main.swift的内容 – DPlusV 2014-11-06 19:50:50

回答

7

你main.swift文件将需要看起来像这样:

import Foundation 
import UIKit 

// Your initialization code here 

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

UIApplicationMain将启动事件循环,并阻止应用退出。

C_ARGCC_ARGV是Swift变量,代表通过main传入的C参数,即int argcchar *argv[]

UPDATE 2016年1月2日: C_ARGCC_ARGV已经分别替换为Process.argcProcess.unsafeArgv。 [source]

3

雨燕1.2格式:

UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate))

2

为很好地回答了来自Darrarski问题How do I access program arguments in Swift?的...

雨燕3语法是:

// 
// 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)) 

并且不要忘记从AppDelegate.swift中删除“@UIApplicationMain”,因为它会被缩小nt并导致编译器错误。

相关问题