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

半腔热情 提交于 2019-12-03 06:40:36

问题


After installing Xcode 8 beta 6, I'm getting a warning saying:

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

in my App Delegate.

There are 2 suggested fixits to silence the warning:

  1. Mark the method as private
  2. Add @nonobjc to the method

Doing either silences the warning. But why does this need to be done?


回答1:


iOS 12 SDK Update

In the iOS 12 SDK (that ships with Xcode 10), UIApplicationLaunchOptionsKey has now been renamed to the nested type UIApplication.LaunchOptionsKey, so you'll want:

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

iOS 10 and 11 SDKs (Xcode 8 and 9)

This warning is due to the fact that the didFinishLaunchingWithOptions: parameter of the application(_:didFinishLaunchingWithOptions:) delegate method is now bridged to Swift as a [UIApplicationLaunchOptionsKey: Any]?, rather than an [NSObject : AnyObject]?.

Therefore you'll need to update your implementation to reflect this change:

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

Note that neither of Xcode's suggested fixes will actually fix the problem, they'll only conceal your implementation of application(_:didFinishLaunchingWithOptions:) from Objective-C – meaning that it'll never actually get called.




回答2:


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  

or the New Syntax

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

you can get the latest Documentation from apple and sample link in here



来源:https://stackoverflow.com/questions/38972440/application-didfinishlaunchingwithoptions-nearly-matches-optional-requireme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!