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 -
'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
}
[someObject observationInfo]
returnnil
if there is no observer.Put a try catch around your removeObserver call
FWIW,
[someObject observationInfo]
seems to benil
ifsomeObject
doesn't have any observers. I wouldn't trust this behavior, however, as I haven't seen it documented. Also, I don't know how to readobservationInfo
to get specific observers.I am not a fan of that try catch solution so what i do most of the time is that i create a subscribe and unsubscribe method for a specific notification inside that class. For example these two methods subcribe or unsubscribe the object to the global keyboard notification:
Inside those methods i use a private property which is set to true or false depending on the subscription state like so: