so go to this link : example
about 2/3rds down the page they have this code:
observation = observe(\.objectToObserve.myDate) { object, change in
//do stuff
}
the code editor there messed up the formatting, and is currently blocking me form editing it. so you'll have to live with the poor formatting or go to the link. It's an example of setting up an NSKeyValueObservation property.
it NEVER works for me if I write the code like that. NEVER. NOT ONCE. And I've tried. tried real hard. It only works if I make modifications:
observation = objectToObserve.observe(\.propertyToObserve, options: [.old, .new]){ (object, change) in{
// do stuff in here.
}
this is the ONLY phrasing I have ever discoved that will allow an NSKeyValueObservation to instantiate.
Just like that. Only that. you have to include options, you have to provide an array of options, the objectToObserve has to be the Object to call the observe function, 'object, change' MUST be embedded in parenthesese. Any other way and I get a compiler error : Type of expression is ambiguous without more context. I wish to understand it, so that I can track down a bigger problem.
after hundreds of successful instantiations of Observers I have one that is instantiated, but never gets notified of changes to the watched object. So I'm suspicious that I my necessary formatting changes have introduced unwanted wrinkles.
here's an In situ example of the method that isn't working:
func shapeLayerAdded(_ layerCon: MDShapeLayerController){
if layerCon.primitives.count > 0{
self.addPrimWidgets(layerCon.primitives, forLayerCon: layerCon)
}
let conObsrvr = layerCon.observe(\.primitives, options: [.old, .new]){ (object, change) in
print("we get the observation")// this never gets called.
if let newArray = change.newValue , let oldArray = change.oldValue{
let addedItems = newArray.filter({ oldArray.contains($0) == false})
let removedItems = oldArray.filter({ newArray.contains($0) == false})
if addedItems.count > 0 {
self.addPrimWidgets(addedItems, forLayerCon: layerCon)
}
if removedItems.count > 0 {
self.removePrimWidgets(removedItems)
}
}
}
contentObsrvrs[layerCon] = conObsrvr // at this point I can confirm that the observer is instantiated and added to the dictionary.
}
my suspicion is that I'm introducing unwanted complexity to the observation by calling the observe function through the objectToObserve, and possibly that is interfering with properly getting notification. As I've said before: I have hundreds of examples of this code working. It's gotten to the point that I can write it in my sleep. But whatever. I know the official examples show it another way, so there might be an issue with the way I am doing it. But, If I were to change it to the official 'blessed' phrasing...
that is a compiler error right there. it won't compile.
The objectToObserve is a descendant of NSObject, and the property we are observing is KVO aware
the MDPrimitive class is an NSClass subclass, and ALL of it's properties are KVO aware.
so... my question is twofold:
1. what is neccessary in order to instantiate an NSKeyValueObservation in the way described in that link? in this current code example I am doing so inside a function, passing the objectToObserve as an argument in the function. Could that be interfering with the code?
2. I've provided all of my offending code. is there anything obvious that jumps out aside of the KeyValueObservation code? I've done tests in a playground to make sure Observers do get notification when I make the kinds of changes I am making to the ObservedObject's Observed property.