Swift сделать расширение протокола наблюдателем уведомлений
Давайте рассмотрим следующий код:
protocol A {
func doA()
}
extension A {
func registerForNotification() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name: UIKeyboardDidShowNotification, object: nil)
}
func keyboardDidShow(notification: NSNotification) {
}
}
Теперь посмотрим на подкласс UIViewController, который реализует A:
class AController: UIViewController, A {
override func viewDidLoad() {
super.viewDidLoad()
self.registerForNotification()
triggerKeyboard()
}
func triggerKeyboard() {
// Some code that make key board appear
}
func doA() {
}
}
Но на удивление это вылетает с ошибкой:
keyboardDidShow:]: нераспознанный селектор, отправленный экземпляру 0x7fc97adc3c60
Так я должен внедрить наблюдателя в самом контроллере представления? Разве это не может остаться в расширении?
Следующие вещи уже пробовали.
делая протокол класса. Добавление keyboardDidShow к самому протоколу в качестве подписи.
protocol A:class {
func doA()
func keyboardDidShow(notification: NSNotification)
}
5 ответов
Я решил аналогичную проблему, внедрив более новую - addObserverForName:object:queue:usingBlock:
метод NSNotificationCenter
и вызов метода напрямую.
extension A where Self: UIViewController {
func registerForNotification() {
NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue: nil) { [unowned self] notification in
self.keyboardDidShow(notification)
}
}
func keyboardDidShow(notification: NSNotification) {
print("This will get called in protocol extension.")
}
}
Этот пример вызовет keyboardDidShow
быть вызванным в расширении протокола.
В дополнение к ответу Джеймса Паолантонио. unregisterForNotification
Метод может быть реализован с использованием связанных объектов.
var pointer: UInt8 = 0
extension NSObject {
var userInfo: [String: Any] {
get {
if let userInfo = objc_getAssociatedObject(self, &pointer) as? [String: Any] {
return userInfo
}
self.userInfo = [String: Any]()
return self.userInfo
}
set(newValue) {
objc_setAssociatedObject(self, &pointer, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}
protocol A {}
extension A where Self: UIViewController {
var defaults: NotificationCenter {
get {
return NotificationCenter.default
}
}
func keyboardDidShow(notification: Notification) {
// Keyboard did show
}
func registerForNotification() {
userInfo["didShowObserver"] = defaults.addObserver(forName: .UIKeyboardDidShow, object: nil, queue: nil, using: keyboardDidShow)
}
func unregisterForNotification() {
if let didShowObserver = userInfo["didShowObserver"] as? NSObjectProtocol {
defaults.removeObserver(didShowObserver, name: .UIKeyboardDidShow, object: nil)
}
}
}
Использование селекторов в Swift требует, чтобы ваш конкретный класс наследовал от NSObject. Чтобы применить это в расширении протокола, вы должны использовать where
, Например:
protocol A {
func doA()
}
extension A where Self: NSObject {
func registerForNotification() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name: UIKeyboardDidShowNotification, object: nil)
}
func keyboardDidShow(notification: NSNotification) {
}
}
Чтобы избежать сбоя, реализуйте метод наблюдателя в классе Swift, который использует протокол.
Реализация должна быть в самом классе Swift, а не только в расширении протокола, поскольку селектор всегда ссылается на метод Objective-C, а функция внутри расширения протокола не доступна в качестве селектора Objective-C. Тем не менее, методы из класса Swift доступны как селекторы Objective-C, если класс Swift наследует от класса Objective-C
Кроме того, в Xcode 7.1, self
должен быть опущен до AnyObject
при указании его в качестве наблюдателя в addObserver
вызов.
protocol A {
func doA()
}
extension A {
func registerForNotification() {
NSNotificationCenter.defaultCenter().addObserver(self as! AnyObject,
selector: Selector("keyboardDidShow:"),
name: UIKeyboardDidShowNotification,
object: nil)
}
func keyboardDidShow(notification: NSNotification) {
print("will not appear")
}
}
class ViewController: UIViewController, A {
override func viewDidLoad() {
super.viewDidLoad()
self.registerForNotification()
triggerKeyboard()
}
func triggerKeyboard(){
// Some code that makes the keyboard appear
}
func doA(){
}
func keyboardDidShow(notification: NSNotification) {
print("got the notification in the class")
}
}
Я решил это используя NSObjectProtocol
как показано ниже,
@objc protocol KeyboardNotificaitonDelegate: NSObjectProtocol {
func keyboardWillBeShown(notification: NSNotification)
func keyboardWillBeHidden(notification: NSNotification)
}
extension KeyboardNotificaitonDelegate {
func registerForKeyboardNotifications() {
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications() {
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
}