0

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!

cacau
  • 3,606
  • 3
  • 21
  • 42
sysco
  • 1
  • 1
  • Why do you want to store the files as a bundle? The user will never see these files/folders. – kubi Mar 03 '10 at 16:09
  • Sorry, I failed to mention that I am using CoreData. So I prefer to store all these references as Strings. Anyways, the user will never sees these files but they will be downloaded and imported to the client app. So having a nice package that can be zipped and passed around is one of my goals. – sysco Mar 06 '10 at 13:12

1 Answers1

0

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
  • 3,201
  • 1
  • 24
  • 16