Xcode 8 beta 6:main.swift不会编译

时间:2016-08-22 21:21:41

标签: swift xcode main swift3 xcode8-beta6

我们有一个自定义的UIApplication对象,所以我们的main.swift是

import Foundation
import UIKit

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

并且在Xcode 8 beta 5中没有用,所以我们使用了这个

//TODO Swift 3 workaround? https://forums.developer.apple.com/thread/46405
UIApplicationMain( Process.argc, UnsafeMutablePointer<UnsafeMutablePointer<CChar>>(Process.unsafeArgv), nil, NSStringFromClass(AppDelegate.self))

在Xcode 8 beta 6上,我们得到使用未解析的标识符&#39;流程&#39;

我们需要在Xcode 8 beta 6 / Swift 3中做些什么才能定义UIApplicationMain?

2 个答案:

答案 0 :(得分:49)

我这样写:

UIApplicationMain(
    CommandLine.argc,
    UnsafeMutableRawPointer(CommandLine.unsafeArgv)
        .bindMemory(
            to: UnsafeMutablePointer<Int8>.self,
            capacity: Int(CommandLine.argc)),
    nil,
    NSStringFromClass(AppDelegate.self)
)

要更改UIApplication类,请将NSStringFromClass(MobileUIApplication.self)替换为该公式中的nil

但是,如果您的目的是将UIApplication子类替换为共享应用程序实例,则有一种更简单的方法:在 Info.plist 中,添加“主类“key并将其值设置为UIApplication子类的字符串名称,并使用@objc(...)属性标记该子类的声明,为其指定相同的Objective-C名称。

编辑此问题现已在iOS 12 / Xcode 10中解决。CommandLine.unsafeArgv现在具有正确的签名,可以轻松调用UIApplicationMain

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

答案 1 :(得分:4)

似乎Process已在测试版6中重命名为CommandLine

CommandLine

但是CommandLine.unsafeArgv的类型与UIApplication的第二个参数不匹配,所以你可能需要写这样的东西:

CommandLine.unsafeArgv.withMemoryRebound(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)) {argv in
    _ = UIApplicationMain(CommandLine.argc, argv, NSStringFromClass(MobileUIApplication.self), NSStringFromClass(AppDelegate.self))
}

(更新)这种不匹配应被视为错误。通常情况下,当您发现“应该不应该”这样的事情时,最好发送bug report,例如测试版5中的第三个参数。我希望很快就能解决这个“错误”。

如果您只想指定自定义UIApplication类,为什么不使用Info.plist?

NSPrincipalClass | String | $(PRODUCT_MODULE_NAME).MobileUIApplication

(在非原始键/值视图中显示为“Principal class”。)

在Info.plist中使用此功能,您可以使用MobileUIApplication以正常方式使用@UIApplicationMain

(ADDITION)UIApplicationMain的标题文档:

// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no
// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.