5

This will save an array (I think).

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:myArray forKey:@"myArray"];
}

Do I have to directly call this method when I want to save an array or do I have to do something else?

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
node ninja
  • 31,796
  • 59
  • 166
  • 254
  • Don't forget to call super: - (void)encodeWithCoder:(NSCoder *)encoder { [super encodeWithCoder:coder]; [encoder encodeObject:myArray forKey:@"myArray"]; } – Magnus Feb 29 '12 at 23:33

1 Answers1

8

You don’t call this method directly. It’s called by a NSCoder subclass if it needs to serialize that object. If you want to encode an object graph use the class methods archivedDataWithRootObject: or archiveRootObject:toFile: of NSKeyedArchiver. This in turn will call the encodeWithCoder: method of your objects. Also note that every object in your array has to implement the NSCoding protocol.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • I am using RMMapper library to set value in UserDefaults. The library uses NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj]; to encode the object but still this method is not called. I have been stuck on this issue for long. Can you help me with this? – Kunal Gupta Oct 27 '16 at 12:40