问题描述
我们有一个自定义的 UIApplication 对象,所以我们的 main.swift 是
We have a custom UIApplication object, so our main.swift was
import Foundation
import UIKit
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(MobileUIApplication), NSStringFromClass(AppDelegate))
这在 Xcode 8 beta 5 中不起作用,所以我们使用了这个
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))
在 Xcode 8 beta 6 上,我们得到 使用未解析的标识符进程"
On Xcode 8 beta 6 we get Use of unresolved identifier 'Process'
我们需要在 Xcode 8 beta 6/Swift 3 中做什么来定义 UIApplicationMain?
What do we need to do in Xcode 8 beta 6/Swift 3 to define the UIApplicationMain?
推荐答案
我是这样写的:
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)
要更改 UIApplication 类,请在该公式中将 NSStringFromClass(MobileUIApplication.self)
替换为 nil
.
To change the UIApplication class, substitute NSStringFromClass(MobileUIApplication.self)
for nil
in that formulation.
但是,如果您在此处唯一的目的是将 UIApplication 子类替换为共享应用程序实例,则有一种更简单的方法:在 Info.plist 中,添加"Principal class" 键并将其值设置为您的 UIApplication 子类的字符串名称,并使用 @objc(...)
属性标记该子类的声明,并赋予它相同的 Objective-C 名称.
However, if your only purpose 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.
EDIT 这个问题现在在 Swift 4.2 中得到解决.CommandLine.unsafeArgv
现在有了正确的签名,并且可以轻松调用 UIApplicationMain
:
EDIT This problem is now solved in Swift 4.2. CommandLine.unsafeArgv
now has the correct signature, and one can call UIApplicationMain
easily:
UIApplicationMain(
CommandLine.argc, CommandLine.unsafeArgv,
nil, NSStringFromClass(AppDelegate.self)
)
这篇关于Xcode 8 beta 6:main.swift 不会编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!