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

前端 未结 2 1379
耶瑟儿~
耶瑟儿~ 2020-12-02 19:10

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

import Foundation
import UIKit

UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFrom         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 19:36

    I write it this way:

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

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

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

提交回复
热议问题