在 iOS 项目中,我实现了以下委托方法来处理应用程序位于前台时的通知:
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.
}
通常,这个警告意味着一个简单的拼写错误,它会导致编译器与协议中的方法规范混淆。但我已经从文档验证了所有这些。
出于好奇,我想检查一下 Xcode 的建议...它是以下委托方法。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (Int) -> Void) {
}
注意转义闭包中的
Int
参数。但这是错误的,对吧? Apple 文档(上面链接)明确指出转义闭包的参数是 UNNotificationPresentationOptions
。并且 UNNotificationPresentationOptions
不是一个枚举(它是一个结构),因此闭包不能采用 Int
,但 Xcode 建议这样做。
Xcode也给出了以下建议:
Make 'userNotificationCenter(_:willPresent:withCompletionHandler:)' private to silence this warning
但是为什么呢?我已经实现了正确的委托方法。我该如何解决这个警告?
编辑1: 这是一个 google Drive 链接,其中包含我正在开发的 Xcode 项目的小型版本。下载 zip 文件后,请按照以下步骤设置并重现警告:
completionHandler(Int(UNNotificationPresentationOptions.banner.rawValue))