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!!
In the header file indicate that your class will implement the
NSCodingprotocol, like<NSCoding>In the
encodeWithCodermethod you need to encode all the fields you want to save like so:Then in the
initWithCodermethod, decode the fields that were encoded:Be sure that any encoded containers only contain objects that also implement the
NSCodingprotocol. This could be core classes such asNSString,NSNumber,NSArray,NSDictionary, as well as your own custom object.If your project is not using garbage collection you need to retain or copy the data retrieved from the archive like so: