views:

39

answers:

1

Suppose I have an model object class Box. In my Box class I add images references (png), audio (mp3) etc... Rather than store them as NSData it seems better to reference the paths to the files...to save memory.

I would like archive this Box class. On the desktop we would use Document Packages (NSFilewrapper). But this class is not part of the Iphone OS.

Any suggestions on archiving this class and including all the files as 'document package'? This is similar to the way Applications appear as a file but are actually a folder... Thanks!

A: 

If you really need to save the objects in Box, I would load them into an NSDictionary and the write that out:

Note: this is untested/non-production code intended to be a starting point only.

- (BOOL)saveBox:(Box*)aBox;
{
    NSMutableDictionary *boxDict = [NSMutableDictionary dictionaryWithCapacity:10];

    // Add contents of box to dictionary (exercise left up to original poster)

    // boxDict is now populated

    // write dictionary to apps document directory.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }

    // Assumes Box class has name property
    NSString *outputFile = [documentsDirectory stringByAppendingPathComponent:[aBox name]];

    return [boxDict writeToFile:outputFile atomically:YES];

}

This could easily be modified to return the name of the file, etc. based on your needs.

Chip Coons
Thanks....this should work nicely!
sysco