Re: Checking document structure in NSDocument based app
Re: Checking document structure in NSDocument based app
- Subject: Re: Checking document structure in NSDocument based app
- From: Seth Willits <email@hidden>
- Date: Tue, 27 Nov 2012 12:24:47 -0800
On Nov 27, 2012, at 10:08 AM, Luc Van Bogaert wrote:
> I've added bindings using keypaths that don't even exist in these early documents.
>
> I'm thinking that I may need to subclass NSDocumentController and override addDocument: so I could check against the document version before allowing the app to actually open it.
>
> ...
Changing the properties of your document class does not mean you can't read older document files.
The document class's properties do not dictate which files can be read, you simply need to determine the version of the file being loaded and massage the data in the file to fit into your new in-app document model. That may mean:
- Adding default values for new properties added to the app which are not in old files
- Ignoring old unused data
- Shuttling values from old key names to new key names (ie if you're using Core Data or a keyed unarchiver like Quincey mentioned).
- Transforming data from one format into another. (ie the old format maybe used an array of strings, and now you need to turn it into an array of dictionaries)
- etc
None of this requires subclassing NSDocumentController or involves bindings, etc. You simply need to read the data from the file, and transfrorm it to fit into your new model. It's usually a very straightforward process.
To do this, however you need to be able to distinguish between different file format versions. Typically the file will have some kind of version number, or magic cookie bytes in the file's "header." If your old version has neither of those, you can add it to the new version. When you read a file from disk, look for the magic cookie and version number and validate it. If it doesn't make any sense (ie the cookie is wrong or the version number is wacky), you can assume it's the old file format.
--
Seth Willits
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden