ios - Swift: adding / removing observers in init / deinit raise error in Interface Builder -
i've simple swift ui component (https://github.com/mbigatti/bmxcircularbutton) inspectable. in init
/ deinit
methods have calls start , stop observation of highlighted state.
init(frame: cgrect) { super.init(frame: frame) self.addobserver(self, forkeypath: "highlighted", options: .new, context: nil) } init(coder adecoder: nscoder!) { super.init(coder: adecoder) self.addobserver(self, forkeypath: "highlighted", options: .new, context: nil) } deinit { self.removeobserver(self, forkeypath: "highlighted") }
doing raise errors interface builder following errors:
file:///users/max/progetti/statusapp/circularbutton/circularbutton/view.xib: error: live views: failed update auto layout status: interface builder cocoa touch tool raised "nsrangeexception" exception: cannot remove observer <_ttc17bmxcircularbutton14circularbutton 0x10ba98e00> key path "highlighted" <_ttc17bmxcircularbutton14circularbutton 0x10ba98e00> because not registered observer. file:///users/max/progetti/statusapp/circularbutton/circularbutton/view.xib: error: live views: failed render instance of _ttc17bmxcircularbutton14circularbutton: interface builder cocoa touch tool raised "nsrangeexception" exception: cannot remove observer <_ttc17bmxcircularbutton14circularbutton 0x10bd055d0> key path "highlighted" <_ttc17bmxcircularbutton14circularbutton 0x10bd055d0> because not registered observer.
it bug or i'm missing something?
as indicated kevin, best answer here use didset and/or willset in class override since you're adding observer self.
override var highlighted:bool { didset { // put observervalue code here } }
Comments
Post a Comment