Re: Cocoa ? XML ? NSDictionary
Re: Cocoa ? XML ? NSDictionary
- Subject: Re: Cocoa ? XML ? NSDictionary
- From: Chris Hanson <email@hidden>
- Date: Mon, 3 Feb 2003 22:12:15 -0600
At 4:24 AM -0800 2/3/03, email@hidden wrote:
However I don't know how to get my own classes contained in the
NSDictionary to
write as XML. Don't know what method to implement or override.
writeToFile:atomically: ain't it ... never gets called.
I'd have expected the XML coding to come in on the NSCoder / NSCoding side,
perhaps as a subclass thereof.
Do I have to chuck the NSDictionary writeToFile:atomically: convenience
function and construct my own CFBlahBlahXMLTree? If so, any hints?
There are a few strategies you could use.
First off, you could do just what you say: Construct your own
CFXMLTree "by hand" and write that to a file.
Another thing you could do is write your own NSCoder subclass that
constructs the CFXMLTree, and use it to encode your object graph.
A third strategy, and the one I tend to prefer for new file formats,
is to use something equivalent to the old EOPropertyListEncoding
protocol. This protocol had three methods:
- (void)encodeIntoPropertyList:(NSMutableDictionary *)plist;
- (id)initWithPropertyList:(NSDictionary *)plist owner:(id)owner;
- (void)awakeWithPropertyList:(NSDictionary *)plist;
To encode an object as an NSDictionary, you send it
-encodeIntoPropertyList: with a mutable NSDictionary to fill in. It
should set keys in the dictionary as appropriate, encoding any
contained objects as sub-dictionaries and so on.
Decoding an object from an NSDictionary requires two steps. First
you allocate an object and initialize it with
-initWithPropertyList:owner:. You do the same with any other
interrelated objects. Then you send the objects
-awakeWithPropertyList: in the same order in which they were
initialized. This two-step process is necessary to make sure that
cross-object references (relationships other than containment) can be
maintained.
In practice, this isn't too hard to implement. It means you can do
things like construct test files by hand in Property List Editor and
still use -[NSDictionary writeToFile:atomically:] to write your data
to a file.
You can read more in the old EOF 4.5 for Objective-C documentation here:
<
http://developer.apple.com/techpubs/webobjects/WebObjects_4.5/System/Library/Frameworks/EOAccess.framework/ObjC_classic/Protocols/EOPropertyListEncoding.html>
This should really be part of Cocoa. (Along with the rest of EOF, darn it!)
-- Chris
--
Chris Hanson, bDistributed.com, Inc. | Email: email@hidden
Custom Application Development | Phone: +1-847-372-3955
http://bdistributed.com/ | Fax: +1-847-589-3738
http://bdistributed.com/Articles/ | Personal Email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.