Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Xcode 6 iOS应用程序中实现main.swift?_Ios_Xcode_Swift - Fatal编程技术网

如何在Xcode 6 iOS应用程序中实现main.swift?

如何在Xcode 6 iOS应用程序中实现main.swift?,ios,xcode,swift,Ios,Xcode,Swift,在我的ios(swift)应用程序中,我创建了main.swift,在其中我设置了一个全局变量来检查NSDefault以检查删除的广告 然后在每个viewcontroller中,我首先检查该全局变量,并在显示视图之前删除广告(如果合适) 问题是xcode不喜欢AppDelegate.swift中的@UIApplicationMain,因为我有一个main.swift。如果我删除@UIApplicationMain行,应用程序将在启动时崩溃 我是否实现了main.swift错误?您的main.sw

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

然后在每个viewcontroller中,我首先检查该全局变量,并在显示视图之前删除广告(如果合适)

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


我是否实现了main.swift错误?

您的main.swift文件需要如下所示:

import Foundation
import UIKit

// Your initialization code here

UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(AppDelegate))
UIApplicationMain
将启动事件循环并阻止应用程序退出

C_ARGC
C_ARGV
是表示通过main传入的C参数的Swift变量,即
int ARGC
char*ARGV[]

更新2016-01-02:
C_ARGC
C_ARGV
分别被
Process.ARGC
Process.gv
取代。[]Swift 1.2格式:


UIApplicationMain(Process.argc、Process.gv、nil、NSStringFromClass(AppDelegate))

作为对Darraski问题的非常好的回答

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))
//
//梅因·斯威夫特
//
进口基金会
导入UIKit
//加载后的第一个语句。。当前时间
let WaysStartTime=CFAbsoluteTimeGetCurrent()
//为UIApplicationMain()的调用生成参数
设argc=CommandLine.argc
设argv=unsafemtablerawpointer(CommandLine.unsafeArgv).bindMemory(to:unsafemtablepointer.self,capacity:Int(CommandLine.argc))
//启动主循环
UIApplicationMain(argc、argv、nil、NSStringFromClass(AppDelegate.self))

不要忘记从AppDelegate.swift中删除“@UIApplicationMain”,因为它是多余的,并且会导致编译器错误。

这就是
main.swift
swift 5中的外观

Hardy_德国的回答在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))

同样,不要忘记从AppDelegate中删除
@UIApplicationMain
标记

请发布main.swift的内容