NotificationCenter.default.addobserver(forName: .NSManagedobjectContextObjectsDidChange,object: moc,queue: nil) { note in // implementation }
我后来删除了名字,如下所示:
NotificationCenter.default.removeObserver(self,name: NSNotification.Name.NSManagedobjectContextObjectsDidChange,object: moc)
我的问题
这够了吗?或者我是否绝对需要将NSObjectProtocol保存到它自己的属性并使用以下语法删除该属性?
NotificationCenter.default.removeObserver(didChangeNotification)
从https://developer.apple.com/reference/foundation/nsnotificationcenter/1411723-addobserverforname开始:
Return Value
An opaque object to act as the observer.
当您调用removeObserver方法中的任何一个时,第一个参数是要删除的观察者.当您设置一个块来响应通知时,self不是观察者,NSNotificationCenter会在幕后创建自己的观察者对象并将其返回给您.
Note: as of iOS 9,you are no longer required to call
removeObserver
fromdealloc
/deinit
,as that will happen automatically when the observer goes away. So,if you’re only targeting iOS 9,this may all just work,but if you’re not retaining the returned observer at all,the notification Could be removed before you expect it to be. Better safe than sorry.
原文地址:https://www.jb51.cc/swift/319136.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。