无法将“ AppDelegate”分配给“ UNUserNotificationCenterDelegate”

use*_*767 0 ios swxmlhash swift3 xcode9

我正在关注本教程:https : //medium.com/flawless-app-stories/ios-remote-push-notifications-in-a-nutshell-d05f5ccac252

但是由于某种原因,我越来越

无法将“ AppDelegate”分配给“ UNUserNotificationCenterDelegate”。

那条线是做什么的?如果我将其注释掉,其余代码将正常工作,并且如果用户希望允许通知,则会提示用户。

func registerForPushNotifications() {
    UNUserNotificationCenter.current().delegate = self // line in question
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
        (granted, error) in
        print("Permission granted: \(granted)")
        // 1. Check if permission granted
        guard granted else { return }
        // 2. Attempt registration for remote notifications on the main thread
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

The*_*ger 5

您首先需要import与班级中的相关delegate (UNUserNotificationCenterDelegate)人员AppDelegate类似以下代码。

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
Run Code Online (Sandbox Code Playgroud)

在您的代码中,UNUserNotificationCenter.current().delegate = self自我没有响应要求delegate,这就是您收到错误消息的原因。