NSNotificationCenter在Swift中添加addObserver,同时调用私有方法 [英] NSNotificationCenter addObserver in Swift while call a private method
本文介绍了NSNotificationCenter在Swift中添加addObserver,同时调用私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 addObserver
API来接收通知:
I use the addObserver
API to receive notification:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOFReceivedNotication:", name:"NotificationIdentifier", object: nil)
,我的方法是:
func methodOFReceivedNotication(notification: NSNotification){
//Action take on Notification
}
是的,它有效!
,但我将方法 methodOFReceivedNotication
更改为私人:
yes,it works!
but while I change method methodOFReceivedNotication
to private:
private func methodOFReceivedNotication(notification: NSNotification){
//Action take on Notification
}
xCode发给我一个错误:无法识别的选择器发送到实例
xCode send me an error: unrecognized selector sent to instance
如何调用私有方法目标是自我
?我不想将 methodOFReceivedNotication
方法暴露给任何其他人。
how to call a private method while the target is self
? I doesn't want to expose methodOFReceivedNotication
method to any other.
推荐答案
只需使用动态
修饰符进行标记,或在方法声明中使用 @objc
属性
Just mark it with the dynamic
modifier or use the @objc
attribute in your method declaration
dynamic private func methodOFReceivedNotication(notification: NSNotification){
//Action take on Notification
}
或
@objc private func methodOFReceivedNotication(notification: NSNotification){
//Action take on Notification
}
这篇关于NSNotificationCenter在Swift中添加addObserver,同时调用私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文