Xcode 8 beta 6:main.swift无法编译



我们有一个自定义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中我们得到使用未解析的标识符'Process'

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

我这样写:

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

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

但是,如果这里的only目的是替换一个UIApplication子类作为共享的应用程序实例,有一个更简单的方法:在Info中。plist,添加"Principal class"键并将其值设置为您的UIApplication子类的字符串名称,并标记该子类的声明与@objc(...)属性给予相同的Objective-C名称。

EDIT这个问题现在在Swift 4.2中解决了。CommandLine.unsafeArgv现在有了正确的签名,可以很容易地调用UIApplicationMain:

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

似乎Process已在beta 6中重命名为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))
}

(UPDATE)这个不匹配应该被认为是一个bug。通常,当您发现"这不应该"的东西时,您最好发送一个bug报告,比如beta 5中的第三个参数。我希望这个"bug"能尽快修复。


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

NSPrincipalClass | String | $(PRODUCT_MODULE_NAME).MobileUIApplication

(在非原始键/值视图中显示为"主类")

在你的信息。您可以使用@UIApplicationMain以正常方式使用您的MobileUIApplication

(add) 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.

最新更新