application(_:didFinishLaunchingWithOptions:)' nearly matches optional requirement
Asked Answered
F

2

27

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?

Fournier answered 16/8, 2016 at 10:25 Comment(0)
A
53

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.

Alcohol answered 16/8, 2016 at 11:5 Comment(6)
Yes, as new implementation doesn't get options parameter, as well as doesn't return Boolean from latest SDKVesicate
The fix suggested here using UIApplicationLaunchOptionsKey doesn't suppress the warning in my project in Xcode 8 beta 6 either, so currently I have to live with that warning.Engelhart
@CodePlumber Hmm interesting, it's working fine for me in Xcode 8 beta 6 – are you sure your launchOptions argument is a [UIApplicationLaunchOptionsKey: Any]?? (It's possible you may have forgotten to change AnyObject to Any). Also check you're not missing the omitted external argument label for the application parameter (if in doubt, you can copy and paste the signature from the answer). If it's still not working, you may want to try cleaning your build folder.Alcohol
You're right, Hamish! It should be Any not AnyObject.Engelhart
I had followed the Xcode suggested fixes and then couldn't figure out why code in didFinishLaunching wasn't running. Thank you for this!!Aspen
The many changes required to maintain code between subsequent versions of Xcode are pure madness IMHO! Apple should improve its game, for this causes a lot of effort.Marketa
G
3

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

Goldie answered 16/8, 2016 at 10:31 Comment(2)
I just realised that when I posted the question, the underscore did not show up, so i have edited it. The method signature has always had the _. This warning only showed up on Xcode 8 beta 6. It was not there in previous betas of Xcode 8Fournier
Yeah, those warning sometimes more confuse than helpVesicate

© 2022 - 2024 — McMap. All rights reserved.