3

Possibly a nooby question - how is encodeWithCoder called? I need to use this to save objects in files on the iphone, I am just unsure of how it is actually called. Do I have to manually call it? If so, what do I use as input for the NScoder argument?

zoul
  • 102,279
  • 44
  • 260
  • 354
Fitzy
  • 1,871
  • 6
  • 23
  • 40

1 Answers1

5

You don’t call it yourself, you use NSKeyedArchiver instead:

NSData *serialized = [NSKeyedArchiver archivedDataWithRootObject:foo];

Where foo is your object that conforms to NSCoding. There’s also a method to save the archived object directly to a file.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • But does that mean if I wanted to archive something like a simple NSArray it would have to conform to NSCoding? This seems a bit overcomplicated to me. – Fitzy Oct 14 '12 at 08:44
  • I'm unable to edit the post, but it's archivedDataWithRootObject not archiveDataWithRootObject – Seth Spitzer Mar 05 '14 at 21:57