Re: Need help with serialization using custom class objects
Re: Need help with serialization using custom class objects
- Subject: Re: Need help with serialization using custom class objects
- From: Fritz Anderson <email@hidden>
- Date: Sat, 4 Jun 2005 17:32:48 -0500
On 3 Jun 2005, at 3:41 PM, James wrote:
What I don't understand is using
NSPropertyListSerialization to serialize the data to a plist format
and also
reading data from that plist back into an array of MyBook objects.
You may be misconceiving what NSPropertyListSerialization will do for
you. Its only service is to transform NSArrays, NSDictionaries, etc.,
into NSData blobs that contain (for instance) XML data that describe
the property lists. The NSData that results is suitable for writing
to a plist file (see -[NSData writeToFile:atomically: and friends).
(NSPropertyListSerialization also makes the reverse translation.) The
translation of your data into the limited vocabulary of the property-
list types is up to you.
I want to save my data as a plist rather than a simple archive for
expanded
compatibility and also because it seems to be a better way to save
data. I
just don't understand how to fit this serialization in with my current
setup. I want to keep the array of MyBook objects for use with the
NSTableView, but I don't see how to serialize the data in the array
and load
it back. I know that NSDictionaries are usually used with
serialization.
Should I convert my MyBook objects into NSDictionaries to serialize
and then
from NSDictionaries back into MyBook objects for use in the
datasource?
Your strategy should be to implement two methods in MyBook:
- (id) asPropertyList;
- (id) initWithPropertyList: (id) aPropertyList;
The first might allocate an autoreleased NSMutableDictionary and
populate it with NSStrings, NSNumbers, NSData, NSArrays, etc.,
derived from MyBook's instance variables. The second would initialize
those instance variables from such a dictionary.
Your catalog's -asPropertyList might populate an autoreleased
NSMutableArray with the collected results of your [MyBook
asPropertyList] messages. If that's all that's in the file, you'd
then do something to translate it into file contents and save it.
You're thinking of using NSPropertyListSerialization, but I'd point
out that both NSDictionary and NSArray implement -
initWithContentsOfFile and -writeToFile:atomically:.
-- F
--
Fritz Anderson
Consulting Programmer
http://resume.manoverboard.org/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden