IOS 警告:实例方法 'userNotificationCenter(_:willPresent:withCompletionHandler:)' 几乎符合可选要求

问题描述 投票:0回答:1

在 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 文件后,请按照以下步骤设置并重现警告:

  1. 下载 zip 文件
  2. 解压文件并将其内容(2 个文件夹 - SampleBuilds 和 SampleWorkspace)放置在 Mac 的 /tmp 文件夹中(这是为了防止放置在主文件夹中时由于路径差异而出现错误)。
  3. 现在,导航到 SampleBuilds->SampleWorkspace->TWInt->IOS_Simulator-arm64
  4. 打开 SampleProject.xcodeproj
  5. 清理 (Cmd + Shift + K) 并构建 (Cmd + B) SampleApp 目标。 (多次构建时,可能不会出现警告。始终清理并构建)
ios swift unusernotificationcenter
1个回答
0
投票

completionHandler(Int(UNNotificationPresentationOptions.banner.rawValue))

© www.soinside.com 2019 - 2024. All rights reserved.