Swift Observer (KVO): Checking for Existence
observationInfo
property is set when there is an observer added
if self.boxView!.observationInfo != nil {
self.boxView!.removeObserver(self, forKeyPath: "center", context: &BoxCenterContext)
}
Apple does not provide any API for check existence of observer but you can manage a Bool flag for this. Like when u register KVO you set isObserver bool to true and then before removing observer you need to check isObserver true of false if isObserver is true so remove observer and if it's false don't do any thing.