I'm binding NSValueTransformer programatically to NSTextField property on demand in a macOS application. My question is how can I remove this binding?
Relevant code snippet to bind the NSValueTransformer:
@IBOutlet var plcAddrLabel: NSTextField!
@objc dynamic var registerAddr = Int(1)
var optionsDict: [NSBindingOption : Any] = [:]
optionsDict = [NSBindingOption.valueTransformer:decToHexTransformer(), NSBindingOption.valueTransformerName:NSValueTransformerName.decToHexTransformerName]
plcAddrLabel.bind(.value, to: self, withKeyPath: "plcRegisterAddress" , options: optionsDict)
When it comes to unbinding I have tried a number of different approaches:
The following code
optionsDict = [NSBindingOption.valueTransformer: "<null>", NSBindingOption.valueTransformerName: "<null>"]
plcAddrLabel.bind(.value, to: self, withKeyPath: "plcRegisterAddress" , options: optionsDict)
causes this error upon execution:
-[NSTaggedPointerString transformedValue:]: unrecognized selector sent to instance 0x7b73031cc07cbe65
The reason for using "<null>" is that it is returned by plcAddrLabel.infoForBinding(.value) before binding the NSValueTransformer for the first time:
NSValueTransformer = "<null>";
NSValueTransformerName = "<null>";
I have also tried
optionsDict = [NSBindingOption.valueTransformer: NSBindingOption.nullPlaceholder, NSBindingOption.valueTransformerName: NSBindingOption.nullPlaceholder]
unfortunately the result is the same.
Interestingly plcAddrLabel.infoForBinding(.value) also shows:
NSNullPlaceholder = "<null>";
It appears, as soon as I try to remove/clear the transformer option the transformer gets called.
Note: while the transformer is bound to the NSTextfield it works fine. Replacing one transformer with another transformer also works.
Any suggestions for further diagnosis would be much appreciated.
Xcode 13.2.1