application(_:didFinishLaunchingWithOptions:)' 几乎符合可选要求

application(_:didFinishLaunchingWithOptions:)' nearly matches optional requirement

安装 Xcode 8 beta 6 后,我收到一条警告:

Instance method 'application(_:didFinishLaunchingWithOptions:)' nearly matches optional requirement 'application(_:didFinishLaunchingWithOptions:)' of protocol 'UIApplicationDelegate'

在我的 App Delegate 中。

有 2 个建议的修正来消除警告:

  1. 将方法标记为私有
  2. 在方法中添加@nonobjc

执行任一操作都会使警告静音。但为什么需要这样做?

the first parameter passed into the function no longer has an external name. This is really just a minor detail since you don’t call this method directly, and it’s a quick fix to make the compiler happy. You can either manually edit that first parameter name to _, or just let Xcode handle this for you.

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool  

或新语法

func application(_ application:UIApplication, 
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool // or remove = nil and try

您可以从 apple and sample link in here

获取最新的文档

iOS 12 SDK 更新

在 iOS 12 SDK(随 Xcode 10 一起提供)中,UIApplicationLaunchOptionsKey has now been renamed 到嵌套类型 UIApplication.LaunchOptionsKey,所以你将想要:

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
    // ...
}

iOS 10 和 11 SDK(Xcode 8 和 9)

此警告是由于 application(_:didFinishLaunchingWithOptions:) 委托方法的 didFinishLaunchingWithOptions: 参数现在桥接到 Swift 作为 [UIApplicationLaunchOptionsKey: Any]?,而不是 [NSObject : AnyObject]?.

因此您需要更新您的实现以反映此更改:

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
    // ...
}

请注意,Xcode 的建议修复都不会真正解决问题,它们只会向 Objective-C 隐藏您对 application(_:didFinishLaunchingWithOptions:) 的实现 – 这意味着它永远不会实际被调用。