2

I am trying to implement local notifications in iOS10, and so far it is not working.

I get this warning around my delegate methods:

Instance method 'userNotificationCenter(_:didReceive:withCompletionHandler:)'     
nearly matches optional requirement 'userNotificationCenter(_:didReceive:withCompletionHandler:)'
of protocol 'UNUserNotificationCenterDelegate'

As solution xcode suggests two options: 1.make it private 2.make it @nonobjc

Why is that? Why do I need to do it? And most important, how to make these methods work?

enter image description here

Community
  • 1
  • 1
Async-
  • 3,140
  • 4
  • 27
  • 49

2 Answers2

3

Xcode 8 beta 6 introduced many Swift 3 changes, one of which is the @escaping keyword to mark closures that might be used after a method returns. Completion handlers are usually escaping closures, because you might show some information to the user, wait for feedback, and then call the completion handler – the method will return and keep a reference to the closure, rather than blocking.

I expect there will be an Xcode 8 GM issued in the next few hours, but right now Xcode doesn't insert the @escaping attribute where it's needed, which is probably what's causing your error. The two Fix-its being offered are red herrings, I'm afraid.

Try writing your method like this instead:

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

Edit: Xcode 8 GM is now available, and correctly inserts @escaping when needed.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
0

I have a line like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {...} 

Xcode gives me the same "nearly matches optional requirement" warning. Xcode 8.0 GM fixit gives me two choices to silence the warning. Add private keyword:

 private func textFieldShouldReturn(textField: UITextField) -> Bool {...} 

or add @nonobjc clause :

@nonobjc func textFieldDidEndEditing(textField: UITextField) {...} 

Either one shuts it up, but I am not sure which one is more preferable. When I tried @ escaping I get an error "@ escaping may only be used on 'parameter' declarations'.

4444
  • 3,541
  • 10
  • 32
  • 43
Rich
  • 121
  • 1
  • 2
  • 7