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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39088928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:10:12  来源:igfitidea点击:

Xcode 8 beta 6: main.swift won't compile

swiftxcodemainswift3xcode8-beta6

提问by Jason Hocker

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

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

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

这在 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))

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

在 Xcode 8 beta 6 上我们得到Use of unresolved identifier 'Process'

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

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

回答by matt

I write it this way:

我是这样写的:

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

To change the UIApplication class, substitute NSStringFromClass(MobileUIApplication.self)for nilin that formulation.

要改变UIApplication类,替代NSStringFromClass(MobileUIApplication.self)nil该配方。

However, if your onlypurpose here is to substitute a UIApplication subclass as the shared application instance, there's an easier way: in the Info.plist, add the "Principal class" key and set its value to the string name of your UIApplication subclass, and mark your declaration of that subclass with an @objc(...)attribute giving it the same Objective-C name.

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

EDITThis problem is now solved in Swift 4.2. CommandLine.unsafeArgvnow has the correct signature, and one can call UIApplicationMaineasily:

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

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

回答by OOPer

It seems Processhas been renamed to CommandLinein beta 6.

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

CommandLine

命令行

But the type of CommandLine.unsafeArgvis mismatching the second argument of UIApplication, so you may need to write something like this:

但是 的类型与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)This mismatching should be considered as a bug. Generally, you'd better send a bug reportwhen you find "this-should-not-be" things, like the third parameter in beta 5. I hope this "bug" will be fixed soon.

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



If you just want to designate your custom UIApplication class, why don't you use Info.plist?

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

NSPrincipalClass | String | $(PRODUCT_MODULE_NAME).MobileUIApplication

(Shown as "Principal class" in non-Raw Keys/Values view.)

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

With this in your Info.plist, you can use your MobileUIApplicationwith normal way using @UIApplicationMain.

在您的 Info.plist 中,您可以MobileUIApplication使用@UIApplicationMain.

(ADDITION) Header doc of 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.
// 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.