IOS warning: instance method 'userNotificationCenter(_:willPresent:withCompletionHandler:)' nearly matches optional requirement

In an iOS project, I have implemented the following delegate method to handle Notifications when app is in foregorund:

func userNotificationCenter (_ pNotificationCenter : UNUserNotificationCenter, willPresent pNotification : UNNotification, withCompletionHandler pCompletionHandler : @escaping (UNNotificationPresentationOptions) -> Void) -> Void
{
    // When app is in foreground, notification is directly sent to 
    // the app by invoking this delegate method. 
}

When building, I'm getting the warning. But my implementation is correct according to documentation.

Why is this method not recognized as the implementation of the UNUserNotificationCenterDelegate protocol?

This is the warning in text format:

Showing All Issues
<file path & line number>: Instance method 'userNotificationCenter(_:willPresent:withCompletionHandler:)' nearly matches optional requirement 'userNotificationCenter(_:willPresent:withCompletionHandler:)' of protocol 'UNUserNotificationCenterDelegate'

This is the image of the warning:

Am I missing some flag in a compiler setting?

My env: Xcode 15.2, Swift 5.0, Deployment target - iOS 14.0+.

Replies

That’s weird. I tried your code in an otherwise empty class and got no warning. Can you show how the class itself is declared: the superclass, other protocols implemented, any attributes like @MainActor, etc.? Do you get the same warning if you implement this protocol method in an otherwise empty class in your project?

  • I created a new empty project manually, created a class conforming to UNUserNotificationCenterDelegate and implemented this method - I don't get any warnings. But in a cmake generated project, I get these warnings. The above warnings are in a cmake generated Xcode project (apologies for not mentioning). But any idea what could be the difference?

Add a Comment