6

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?

1
  • Don't forget to call super: - (void)encodeWithCoder:(NSCoder *)encoder { [super encodeWithCoder:coder]; [encoder encodeObject:myArray forKey:@"myArray"]; } Commented Feb 29, 2012 at 23:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.