iOS:userNotificationCenter代理方法匹配警告的解决咨询
iOS前台通知代理方法匹配警告的解决疑问
我在iOS项目中实现了处理APP前台通知的UNUserNotificationCenterDelegate代理方法,代码如下:
func userNotificationCenter (_ pNotificationCenter : UNUserNotificationCenter, willPresent pNotification : UNNotification, withCompletionHandler pCompletionHandler : @escaping (UNNotificationPresentationOptions) -> Void) -> Void { // 当APP处于前台时,通知会通过调用这个代理方法直接发送给APP }
编译时出现警告:"instance method 'userNotificationCenter(_:willPresent:withCompletionHandler:)' nearly matches optional requirement"。通常这类警告是协议方法拼写不匹配导致的,但我对照Apple官方文档确认代码完全无误。然而Xcode给出的修复建议存在错误——它推荐的方法将闭包参数写成了Int而非正确的UNNotificationPresentationOptions,还建议把该方法设为私有来消除警告。明明实现的是正确的代理方法,该如何解决这个警告?
可复现问题的操作步骤:
- 下载压缩包
- 解压后将
SampleBuilds和SampleWorkspace两个文件夹放入Mac的/tmp文件夹(避免路径差异引发错误) - 进入
SampleBuilds->SampleWorkspace->TWInt->IOS_Simulator-arm64目录 - 打开
SampleProject.xcodeproj - 清理(Cmd + Shift + K)并构建(Cmd + B)
SampleApptarget(多次构建可能不出现警告,需每次清理后再执行构建)
内容的提问来源于stack exchange,提问作者NightFuryLxD
相关产品推荐
相关产品推荐

