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?

Hamish
  • 69,859
  • 16
  • 167
  • 245
Aero
  • 390
  • 4
  • 9

2 Answers2

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.

Hamish
  • 69,859
  • 16
  • 167
  • 245
  • 1
    Yes, as new implementation doesn't get options parameter, as well as doesn't return Boolean from latest SDK – pedrouan Aug 18 '16 at 13:18
  • 1
    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. – CodeBrew Aug 28 '16 at 21:52
  • 2
    @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. – Hamish Aug 28 '16 at 21:59
  • You're right, Hamish! It should be `Any` not `AnyObject`. – CodeBrew Aug 28 '16 at 22:01
  • I had followed the Xcode suggested fixes and then couldn't figure out why code in didFinishLaunching wasn't running. Thank you for this!! – RanLearns Sep 04 '16 at 23:52
  • 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. – Drux Sep 23 '16 at 06:23
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

Anbu.Karthik
  • 77,564
  • 21
  • 153
  • 132
  • 1
    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 8 – Aero Aug 16 '16 at 11:05
  • Yeah, those warning sometimes more confuse than help – pedrouan Aug 18 '16 at 13:18