Re: Property List Question
Re: Property List Question
- Subject: Re: Property List Question
- From: mmalcolm crawford <email@hidden>
- Date: Thu, 17 May 2001 19:26:30 +0100
On Thursday, May 17, 2001, at 09:45 am, Stefan Arentz wrote:
The problem is is that I want to represent the 'people' as objects
(for the obvious OO benefits). However, if I do, I will lose the
ability to use "arrayWithContentsOfFile:" and
"writeToFile:atomically:" because the array will no longer contain
only property list objects.
Is there a protocol or something which I can make the 'people' objects
conform to which will make the saving and loading work?
There is a great API for that in Foundation. Just implement the
NSCoding protocol and you're all set.
Umm, not necessarily...
It wasn't entirely clear to me if the original question was how to store
"people" as property lists, or as serialised objects. On balance
(noting "So I want to represent it as an array of arrays of *strings*")
I guessed the former. If I was wrong then certainly NSCoding will be an
easy answer, but still not necessarily the best.
NSArchived objects are stored in a binary representation, which means
they can't be readily inspected, or edited, in anything other than a
Cocoa app. If you want the ability to edit the data, or to be able to
exchange this data with other applications, producing a string-based
property list representation will be a better solution.
If you're be writing a Java-based Cocoa app, then unfortunately at the
moment the chances fair that are you will be unable to use NSArchiving
anyway (some of the serialisation methods are broken).
The other more subtle issue that plists allow you to address more
readily (and Sketch's example in particular shows) is that you have more
flexibility to allow for backwards compatibility.
If your data model is subject to change (you add instance variables
etc), then NSArchiving does allow you to store a version number in your
files. You can then write your "read in" methods appropriately to take
account of old versions of the data. What NSArchiving doesn't allow you
to do is save a file in a previous format (e.g. without the instance
variables you added in later versions).
(Thanks to Mike Ferris for pointing some of this out to me a while back.)
What would be very handy would be a Cocoa equivalent of WebObject's
WOXMLCoding. It looks like there might be a start with
NSPropertyListSerialization's XMLDataFromPropertyList, but:
(a) the documentation is incomplete;
(b) it's in Java only;
(c) It doesn't appear to allow the use of a mapping file, which is
extremely useful in WOXMLCoding.
mmalc