NSKeyedArchiver on NSArray has large size overhead

Posted by redguy on Stack Overflow See other posts from Stack Overflow or by redguy
Published on 2010-05-06T02:51:59Z Indexed on 2010/05/06 2:58 UTC
Read the original article Hit count: 434

Filed under:
|
|
|
|

I'm using NSKeyedArchiver in Mac OS X program which generates data for iPhone application. I found out that by default, resulting archives are much bigger than I expected. Example:

NSMutableArray * ar = [NSMutableArray arrayWithCapacity:10];

for (int i = 0; i < 100000; i++) {
    NSString * s = [NSString stringWithFormat:@"item%06d", i];
    [ar addObject:s];
}
[NSKeyedArchiver archiveRootObject:ar toFile: @"NSKeyedArchiver.test"];

This stores 10 * 100000 = 1M bytes of useful data, yet the size of the resulting file is almost three megabytes. The overhead seems to be growing with number of items in the array. In this case, for 1000 items, the file was about 22k.

"file" reports that it is a "Apple binary property list" (not the XML format).

Is there an simple way to prevent this huge overhead? I wanted to use the NSKeyedArchiver for the simplicity it provides. I can write data to my own, non-generic, binary format, but that's not very elegant. Also, aggregating the data into large chunks and feeding these to the NSKeyedArchiver should work, but again, that kinda beats the point of using simple&easy&ready to use archiver. Am I missing some method call or usage pattern that would reduce this overhead?

© Stack Overflow or respective owner

Related posts about nskeyedarchiver

Related posts about overhead