swift2 Swift使协议扩展成为通知观察器

dced5bon  于 2022-11-06  发布在  Swift
关注(0)|答案(5)|浏览(188)

让我们考虑以下代码:

protocol A {
    func doA()
}

extension A {
  func registerForNotification() {
      NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name: UIKeyboardDidShowNotification, object: nil)
  }

  func keyboardDidShow(notification: NSNotification) {

  }
}

现在来看一个实现A的UIViewController子类:

class AController: UIViewController, A {
   override func viewDidLoad() {
      super.viewDidLoad()
      self.registerForNotification()
      triggerKeyboard()
   }

   func triggerKeyboard() {
      // Some code that make key board appear
   }

   func doA() {
   }
}

但令人惊讶的是,它崩溃并出现错误:
键盘ID显示:]:发送到示例0x7fc97adc3c60的选择器无法识别
那么我应该在视图控制器中实现观察器吗?它不能留在扩展中吗?
下面的事情已经试过了。
使A成为一个类协议。将keyboardDidShow添加到协议本身作为签名。

protocol A:class {
   func doA()
   func keyboardDidShow(notification: NSNotification)
}
xe55xuns

xe55xuns1#

我通过实现NSNotificationCenter的较新的- addObserverForName:object:queue:usingBlock:方法并直接调用该方法解决了一个类似的问题。

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

vjrehmav

vjrehmav2#

除了James Paolantonio的答案之外,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)
        }
    }
}
eeq64g8w

eeq64g8w3#

要避免崩溃,请在使用该协议的Swift类中实现观察器方法。
实现必须在Swift类本身中,而不仅仅是在协议扩展中,因为选择器总是引用Objective-C方法,而协议扩展中的函数不能作为Objective-C选择器使用。然而,如果Swift类继承自Objective-C类,那么Swift类中的方法就可以作为Objective-C选择器使用
如果Swift类继承自Objective-C类,则该类中的所有方法和属性都可用作Objective-C选择器。
此外,在Xcode 7.1中,在addObserver调用中将self指定为观察者时,必须将其向下转换为AnyObject

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")
    }
}
1wnzp6jl

1wnzp6jl4#

在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) {

  }
}
xxhby3vn

xxhby3vn5#

我用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)
}
}

相关问题