xcode - Xcode 8 中的警告 : Instance method nearly matches optional requirement

标签 xcode notifications ios10

我正在尝试在iOS10中实现本地通知,到目前为止it is not working

我在委托(delegate)方法周围收到此警告:

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

作为解决方案,xcode 建议两个选项: 1.设为私有(private) 2.使其@nonobjc

这是为什么呢?为什么我需要这样做?最重要的是,如何使这些方法发挥作用?

enter image description here

最佳答案

Xcode 8 beta 6 引入了许多 Swift 3 更改,其中之一是 @escaping 关键字,用于标记方法返回后可能使用的闭包。完成处理程序通常会转义闭包,因为您可能会向用户显示一些信息,等待反馈,然后调用完成处理程序 - 该方法将返回并保留对闭包的引用,而不是阻塞.

我预计将在接下来的几个小时内发布 Xcode 8 GM,但现在 Xcode 不会在需要的地方插入 @escaping 属性,这可能是导致你的错误的原因。恐怕所提供的两个 Fix-it 是转移注意力的。

尝试像这样编写你的方法:

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

编辑:Xcode 8 GM 现已可用,并在需要时正确插入 @escaping

关于xcode - Xcode 8 中的警告 : Instance method nearly matches optional requirement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39369330/

相关文章:

ios - 当应用程序处于 iOS 10 后台时无法访问推送通知

swift - 如何在 Swift 中像数组一样访问图像资源

ios - 来自 iTC 的无效 bundle 错误,无法提交应用程序 - 因为 4 个 swift dylibs 无法嵌入

Windows 10 : is it possible to add new notification to action center manually?

android - 升级到最新的 android/firebase sdk,这个 azure 通知中心示例 - xamarin forms

使用 UIImagePickerController 时 iOS 10 错误 [访问] <private>

ios - 在后台加载数据并重新加载tableView

ios - Xcode 11 中的 iOS 构建失败

email - 快速结帐 (NVP) 中的即时付款通知订单说明空白

iOS 10 无法打开 Spotify 应用