我正在尝试在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
这是为什么呢?为什么我需要这样做?最重要的是,如何使这些方法发挥作用?
最佳答案
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/