如何判断对象是否附加了键值观察器 [英] How can i tell if an object has a key value observer attached

查看:27
本文介绍了如何判断对象是否附加了键值观察器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你告诉一个客观的 c 对象 removeObservers: 对于一个关键路径并且那个关键路径还没有被注册,它会破坏悲伤.喜欢 -

if you tell an objective c object to removeObservers: for a key path and that key path has not been registered, it cracks the sads. like -

'无法从中删除关键路径theKeyPath"的观察者,因为它没有注册为观察者.'

'Cannot remove an observer for the key path "theKeyPath" from because it is not registered as an observer.'

有没有办法确定一个对象是否有一个注册的观察者,所以我可以这样做

is there a way to determine if an object has a registered observer, so i can do this

if (object has observer){
  remove observer
}
else{
  go on my merry way
}

推荐答案

在 removeObserver 调用中尝试捕获

Put a try catch around your removeObserver call

@try{
   [someObject removeObserver:someObserver forKeyPath:somePath];
}@catch(id anException){
   //do nothing, obviously it wasn't attached because an exception was thrown
}

这篇关于如何判断对象是否附加了键值观察器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆