Re: Swift with NSDocument and Revert To... browse all versions.

Bill Pitcher

Thank you for your help with this!

I’ve given it another try and hope some knowledgeable person could check that I’m still not Doing it Wrong ™

Currently WindowController.document still seems to be AnyObject? which caused me some consternation

I’ve switch the model to a NSObject subclass, and now KVOing, it fixes the initial problem I was having with Revert…

any feedback gratefully received
Bill Pitcher

On 14/07/2018, at 2:25 AM, Quincey Morris <quinceymorris@...> wrote:

On Jul 13, 2018, at 03:49 , Bill Pitcher <bill@...> wrote:

Unless I’m mistaken KVO requires going back to NSObject for my model, and not plain Swift classes.
Well, things get more complicated when you use non-Obj-C types, but it’s not necessarily true that you can’t use Swift types. In the case of the model, it may be sufficient that your “model" property of NSDocument be @objc (but not a Swift-native type), to KVO-observe it.

WindowController.document is Any? so doesn’t seem observable but I didn’t spend long on checking that.
It’s still an @obj property of an Obj-C object, so it’s observable. The type is actually ‘id’, which translates into Swift ‘Any’ or ‘Any?’ these days, not into ‘AnyObject’ as it used to.

What I think you are saying is build a KVO compatible model and then link in observers through to all the Controllers.
I wasn’t really saying that, just saying you should be able to use KVO to keep track of the model’s root object. Whether you use KVO or Obj-C compatible objects or properties further down the line is up to you.

Note, however, that there is still a *lot* in built-in pressure to stay Obj-C-compatible in this part of your app design, because a lot of familiar design patterns rely on KVO and KVC.

Join to automatically receive all group messages.