协议扩展覆盖内置类成员函数

时间:2016-11-15 21:42:14

标签: ios swift

protocol PushNotification {
   func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
}

extension PushNotification where Self:AppDelegate {
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        print("Register for Push Notifications Successful")
    }
}

我正在尝试让我的应用代理而不是调用自己的

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)

调用扩展程序,但似乎不起作用? 这甚至可能吗?在swift 2.3?还是迅速3?

预期行为

所以当我写

let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
        application.registerForRemoteNotifications()

应该致电

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) 

属于app委托,我通常必须在app委托中覆盖。

我希望它能调用我的协议扩展名。这是app delegate

class AppDelegate: UIResponder, UIApplicationDelegate, AWSDKDelegate, PushNotification {
// Could this function be called from my protocol extension instead?
// Rather than me overriding this function ?
  //    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
//        print("Register for Push Notifications Successful")
//    }
}

1 个答案:

答案 0 :(得分:0)

协议扩展提供了方法的默认实现。任何实现该方法的符合类型都将覆盖扩展的实现。