views:

64

answers:

1

Hi,

i've got problems using the NSKeyedArchiver/NSKeyedUnarchiver for saving my object.
I added the methods - (id)initWithCoder:(NSCoder *)decoder and - (void)encodeWithCoder:(NSCoder *)encoder. The problem is when i try to save the object it doesn't work correctly.
I could imagine a problem (but I'm not sure if it is the problem ;) ). I have some arrays within my object. They contain more objects (I implemented both of the "Coder"-Methods as well). So does the array call the methods in it's objects?

Any possible solution??

Thanks!!

+3  A: 

Ok well first of all in the .h implement the NSCoding protocol like '<'NSCoding'>' (ignore the ' stackoverflow thinks its and html tag.) Second In your encodeWithCoder: you need to encode all the data you want to save like so:

[encoder encodeObject:array1 forKey:@"array1"];

Then in your initWithCoder you need to decode all your important data like so:

array1 = [coder decodeObjectForKey:@"array1"];

Also be sure that your arrays contain objects that implement NSCoding such as NSString, NSNumber, NSArray, NSDictionary, and other custom objects that you have implemented it.

If you are not using garbage collection you might want to do this instead:

array1 = [[coder decodeObjectForKey:@"array1"] retain];

Justin Meiners
Thanks a lot!!!
Lars Petersen
Well... now I have the next problem: How do I act when I have a object inherited by my other own object. One worked well, but as soon as I inherit it it does not work
Lars Petersen
- (id)initWithCoder:(NSCoder*)coder{if (self = [super initWithCoder:coder]){
Justin Meiners