How can i tell if an object has a key value observer attached

前端 未结 10 1517
萌比男神i
萌比男神i 2020-12-04 06:51

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 observe

10条回答
  •  难免孤独
    2020-12-04 07:13

    In addition to Adam's answer I would like to suggest to use macro like this

    #define SafeRemoveObserver(sender, observer, keyPath) \
    @try{\
       [sender removeObserver:observer forKeyPath:keyPath];\
    }@catch(id anException){\
    }
    

    example of usage

    - (void)dealloc {
        SafeRemoveObserver(someObject, self, somePath);
    }
    

提交回复
热议问题