23

We have a custom UIApplication object, so our main.swift was

import Foundation
import UIKit

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

and that didn't work in Xcode 8 beta 5 so we used this

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

On Xcode 8 beta 6 we get Use of unresolved identifier 'Process'

What do we need to do in Xcode 8 beta 6/Swift 3 to define the UIApplicationMain?

4

2 回答 2

59

我是这样写的:

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 中,添加“Principal class”键并将其值设置为 UIApplication 子类的字符串名称,并使用@objc(...)赋予它相同的 Objective-C 名称的属性标记您对该子类的声明。

编辑这个问题现在在 Swift 4.2 中得到解决。CommandLine.unsafeArgv现在有了正确的签名,可以UIApplicationMain轻松调用:

UIApplicationMain(
    CommandLine.argc, CommandLine.unsafeArgv, 
    nil, NSStringFromClass(AppDelegate.self)
)
于 2016-08-22T22:25:25.643 回答
5

它似乎ProcessCommandLine在 beta 6 中重命名为。

命令行

但是 的类型与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))
}

(更新)这种不匹配应该被视为一个错误。一般来说,当你发现“这不应该”的东西时,你最好发送一个错误报告,比如 beta 5 中的第三个参数。我希望这个“错误”能很快得到修复。


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

NSPrincipalClass | String | $(PRODUCT_MODULE_NAME).MobileUIApplication

(在非原始键/值视图中显示为“主体类”。)

有了这个在你的 Info.plist 中,你可以用MobileUIApplication正常的方式使用你的@UIApplicationMain.

(添加)标题文档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.
于 2016-08-22T22:05:52.047 回答