Ios 在if语句中未调用委托方法

Ios 在if语句中未调用委托方法,ios,swift,swift-protocols,Ios,Swift,Swift Protocols,我已经创建了一个未通知对象,因此可以在我的应用程序中调用多个通知 @available(iOS 10.0, *) class AlertNotifications : NSObject, UNUserNotificationCenterDelegate { weak var notifyDelegate : UNUserNotificationCenterDelegate? override init() { super.init() }

我已经创建了一个未通知对象,因此可以在我的应用程序中调用多个通知

@available(iOS 10.0, *)

class AlertNotifications : NSObject, UNUserNotificationCenterDelegate  {
    weak var notifyDelegate : UNUserNotificationCenterDelegate?

    override init() {
        super.init()
    }

    func registerNotification() {
        // notification authorization code
    }

    private func scheduleNotification() {
        //notification scheduler code
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        print("calling completion")
        completionHandler([.alert, .sound])
    }
}
在我试图设置通知的ViewController上,由于我的应用程序必须与iOS 9和iOS 10兼容,我被迫将调用包装在if语句中

if #available(iOS 10.0, *) {
    let notify = AlertNotifications()
    notify.notifyDelegate = self

    notify.registerNotification()
} else {
    // Fallback on earlier versions

    print("never called delegate")
}

不幸的是,这样做并没有调用由我的
AlertNotification
类中的
UNUserNotificationCenterDelegate
创建的委托方法。我的假设是,代理未正确设置,但我不知道如何修复它。非常感谢您的帮助。

您正在创建的
AlertNotifications
类的实例将在退出if语句后立即被释放。某些内容需要保留对它的强引用。每当我尝试对AlertNotifications进行强引用时,Xcode要求我将其放入if语句中。您正在创建的
AlertNotifications
类的实例将在退出if语句后立即释放。有些东西需要保持对它的强引用。每当我尝试对AlertNotifications进行强引用时,Xcode要求我将其放入if语句中。