objective-cnsarchiving

Building a custom NSArchiver serialize to string


How does NSArchiver serialize to file? I assume it's serialized in binary format, is that correct? What if I want to store it in string so I can store into SQLite database? Do I need to write my own custom NSArchiver? If so, how do I go about doing that? Are there any tutorials out there?

p.s. I do realize Core Data can do this but let me cross that option out for now.


Solution

  • You can archive to an NSData object instead of to a file, if you want, with +archivedDataWithRootObject:. It won't be a "string," but that's fine, because an NSString in Cocoa represents a sequence of Unicode characters, while an NSData represents a sequence of bytes (which you could easily store wherever you want, including in a database).

    Note that you really should be using NSKeyedArchiver instead:

    + (NSData *)archivedDataWithRootObject:(id)rootObject

    + (id)unarchiveObjectWithData:(NSData *)data