我如何知道一个对象是否有一个键值观察者附加 [英] How can i tell if an object has a key value observer attached

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

问题描述

如果你告诉一个目标c对象removeObservers:对于一个键路径和该键路径没有注册,它破解sads。 like -

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调用中加入try catch

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天全站免登陆