View Full Version : Archiving Multiple Objects
davidw
03-27-2007, 12:09 AM
Im trying to Archive some objects and save them.
Just in a standard document based app
- (NSData *)dataRepresentationOfType:(NSString *)aType
How do i make a NSData Object with multple objects?
all i've ever seen is with one.
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
return [NSKeyedArchiver archivedDataWithRootObject:someObject];
}
Here was my attempt
NSKeyedArchiver *saveData;
[saveData encodeObject:[docOvalView getOvalArray] forKey:@"ovalArray"];
[saveData encodeObject:[docOvalView getOvalColorArray] forKey:@"ovalColorArray"];
How do i make it spit out an NSData object?
and how do i load my objects back from the NSData object i will get from
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
bramley
03-27-2007, 07:30 AM
Steve Kochan's Book
Chapter 11 - on protocols (to understand NSCoding which is a protocol)
Chapter 19 - archiving
Hillegass
Chapter 8 deals with saving data and archiving it.
Note all objects save the root object have to be contained within some container or other so just archive that one in accordance with requirements of NSCoding.
Note decoding does not retain objects by default - your code needs to do this.
Also note that naming accessor methods using the form 'getMyAttribute' will probably cause problems in applications that make use of 'Key Value Coding' You should get into the habit of using the form of 'myAttribute' (for accessors) and 'setMyAttribute' (for setters)
davidw
03-27-2007, 02:05 PM
all of those chapters dont appear to deal with NSData much at all.
Im thinking i could do it pretty easily writing to the file myself though.
thanks for the tips, i'll stop using get.
it does seem a little bit cleaner that way though. Its to bad Key Value Coding dosnt use get.
bramley
03-27-2007, 05:54 PM
all of those chapters dont appear to deal with NSData much at all.
The process of archiving/dearchiving objects for the purposes of saving/loading data shouldn't need the developer to do anything with NSData objects at all. i.e. although the process reads and writes NSData objects the act of getting them onto disk is opaque. The developer only needs to get an instance of NSKeyedArchiver/NSKeyedUnarchiver to work with his/her objects. Do it right and out pops the NSData object which an NSDocument already knows how to process.
You only need: -
to implement NSCoding for objects you are using (note Cocoa objects are usually already NSCoding compliant),
ensure that multiple objects are encapsulated inside a container object - an array, a dictionary, set etc. (they should already be), and
that you retain objects as you decode them.
I think you should ignore all other references to archiving that you may have read and just concentrate on what Hillegass and Kochan have to say. The archiving process looks complex but is actually very simple and probably one of the best worked out features in Obj-C/Cocoa.
EDIT Note that you will usually find that it pays to write code using the 'Model-Controller-View paradigm' - see p 121 of Hillegass for his take on this. Note this is a OOPS approach not solely a Cocoa one. Also see http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCTutorial/chapter02/chapter_2_section_3.html
davidw
04-02-2007, 12:12 PM
Thanks alot..
I've got it figured out, for some reason my first instinct wasnt to put them all into a container object, then archive that.
just out of curiosity
do you know what on earth these NSKeyedArchiver methods like encodeObject: are used for then?
bramley
04-02-2007, 03:55 PM
just out of curiosity
do you know what on earth these NSKeyedArchiver methods like encodeObject: are used for then?
They're purpose is to allow NSArchiver (and its subclasses such as NSKeyedArchiver) to encode references to an object instead of the object itself.
This is useful if you had a situation where Object A required that Object B be encoded that required Object C be encoded - which required that Object A be encoded again. Happens quite frequently. NSCoder would be trapped in an unending loop - unless there was a way to simply encode a reference to objects that have already been encoded. encodeObject does this. You can override it in your own subclasses of NSArchiver (or NSKeyedArchiver) for some functionality (what I do not know) but it would be important to remember to allow the original implementation to be called (using [super encodeObject] ) or the situation described above may occur.
EDIT
(what I do not know)
You could use it where you have a "branch" of objects forming part of a larger tree that you want to copy, but maintain links to other objects that you do not want to copy.
/EDIT
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.