3

I am implementing

func userNotificationCenter(_ center: UNUserNotificationCenter, 
    didReceive response: UNNotificationResponse, 
    withCompletionHandler completionHandler: () -> Void) {

But I'm getting the "nearly matches optional requirement" warning from the compiler. What's wrong with my declaration? I copied it right out of the documentation!

matt
  • 515,959
  • 87
  • 875
  • 1,141

1 Answers1

2

It's the @escaping attribute. It isn't shown in the documentation. But it is shown in the header. That's the place to copy from! Here's the correct declaration:

func userNotificationCenter(_ center: UNUserNotificationCenter, 
    didReceive response: UNNotificationResponse, 
    withCompletionHandler completionHandler: @escaping () -> Void) {
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • copied from the comments of [here](http://stackoverflow.com/questions/39395513/how-to-handle-usernotifications-actions-in-ios-10/39426419?noredirect=1#comment66238962_39426419): `@escaping` means "this closure may be executed later". Compiler can easily optimize non-escaping closures which are often used in map, filter and such, Swift 3 has made it default. All completion handlers are executed when some task is completed -- later, so we need to annotate @escaping for all completion handlers in Swift 3 – mfaani Sep 15 '16 at 17:45