Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>NSCoding</code> will do exactly what you want. I recommend you read up on it in the Apple docs, but I thought it was pretty straightforward to use. Your class (and any child classes) will need to implement the <code>NSCoding</code> protocol and you'll need to add <code>-encodeWithCoder:</code> and <code>-initWithCoder:</code> methods to your objects. Most of the common framework classes implement <code>NSCoding</code> already.</p> <p>The code for your class will look something like this:</p> <pre><code>-(void) encodeWithCoder: (NSCoder*) coder { [coder encodeInteger: versionValue forKey: versionKey]; [coder encodeObject: myStuff forKey: myStuffKey]; } -(id) initWithCoder: (NSCoder*) coder { self = [super init]; if ( ! self) return nil; myStuff = [[coder decodeObjectForKey: myStuffKey] retain]; return self; } </code></pre> <p>It's recommended you add a version number when encoding to give you flexibility to manage changes to your archive format in future versions. </p> <p>In my class, I added a convenience method to archive my object:</p> <pre><code>-(void) archiveToFile: (NSString*) path { NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data]; [archiver encodeObject: self forKey: myArchiveKey]; [archiver finishEncoding]; [archiver release]; [data writeToFile: path atomically: YES]; [data release]; } </code></pre> <p>and another one to unarchive or create a new object:</p> <pre><code>+(MyArchive*) newFromFile: (NSString*) path orWithMyStuff: (MyStuff*) myStuff { NSData *data = [[NSData alloc] initWithContentsOfFile: path]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data]; MyArchive *myArchive = [unarchiver decodeObjectForKey: myArchiveKey]; [unarchiver finishDecoding]; if (myArchive) { [myArchive retain]; } else { myArchive = [[MyArchive alloc] initWithStuff: myStuff; } [unarchiver release]; [data release]; return myArchive; } </code></pre> <p>Since your top level object is an <code>NSArray</code>, you'll need to modify the last two methods for your case, but most of the boilerplate code will be the same.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload