0

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.

huggie
  • 17,587
  • 27
  • 82
  • 139

1 Answers1

0

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

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • What about in the Guide (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Archiving/Articles/codingctypes.html#//apple_ref/doc/uid/20001294-96941-BBCDIHEF) where it says "You should not wrap a structure with an NSData object and archive that"? The structure I'm trying to store is a doubly link list in C. I'm in the middle of implementing it, and I am currently just trying to store my inputs to the C API so I could reconstruct it using rudimentary material later. – huggie Aug 25 '12 at 10:02
  • That's talking about what goes *into* the archive, not how you use the archive's resulting bits. Your question seems to be asking "I know how to use an `NSArchiver`, but is there a way to archive to memory and not to a file?" `NSArchiver` is meant for use with Objective-C objects, not C structures, which is what that guide is saying. – John Calsbeek Aug 25 '12 at 16:29
  • Right, I didn't say but my happen to want to wrap a doubly link list written in C, hence the confusion over your reply. – huggie Aug 26 '12 at 09:51