EDIT: Here is a working version. I was able to retrieve my object within the NSMUtableArray after I saved and loaded it from the NSUserDefaults via NSCoding. I think it's important to mention, that you not only need to de-archive the array, but also all its content. As you can see, I had to not only store the NSData of my freeze object, but also the NSData of my array:
// My class "Freeze"
@interface Freeze : NSObject <NSCoding> // The NSCoding-protocoll is important!!
{
NSMutableString *name;
}
@property(nonatomic, copy) NSMutableString *name;
-(void) InitString;
@end
@implementation Freeze
@synthesize name;
-(void) InitString {
name = [[NSMutableString stringWithString:@"Some sentence... lalala"] retain];
}
// Method from NSCoding-protocol
- (void)encodeWithCoder:(NSCoder *)encoder
{
//Encode properties, other class variables, etc
[encoder encodeObject:self.name forKey:@"name"];
}
// Method from NSCoding-protocol
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if( self != nil )
{
//decode properties, other class vars
self.name = [decoder decodeObjectForKey:@"name"];
}
return self;
}
@end
Freeze *freeze;
NSMutableArray *runes;
NSMutableArray *newRunes;
runes = [[NSMutableArray alloc] init];
newRunes = [[NSMutableArray alloc] init];
freeze = [[Freeze alloc] init];
[freeze InitString];
[runes addObject:freeze];
[self saveState];
[self restoreState];
Freeze *newFreeze = [[Freeze alloc] init];
newFreeze = [newRunes objectAtIndex:0];
NSString *String = [NSString stringWithString:newFreeze.name];
NSLog(@"%@", String);
//-----------------------------------------------------------------------------
- (void) saveState
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSData* myClassData = [NSKeyedArchiver archivedDataWithRootObject:freeze];
[defaults setObject:myClassData forKey:@"MyClass"];
NSData* myClassArrayData = [NSKeyedArchiver archivedDataWithRootObject:runes];
[defaults setObject:myClassArrayData forKey:@"MyClassArray"];
}
- (void) restoreState
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSData* myClassData = [defaults objectForKey:@"MyClass"];
freeze = [NSKeyedUnarchiver unarchiveObjectWithData:myClassData];
NSData* myClassArrayData = [defaults objectForKey:@"MyClassArray"];
NSArray *savedMyClassArray = [NSKeyedUnarchiver unarchiveObjectWithData:myClassArrayData];
if( savedMyClassArray != nil )
newRunes = [[NSMutableArray alloc] initWithArray:savedMyClassArray];
else
newRunes = [[NSMutableArray alloc] init];
}
EDIT: This is an error I got before, it doesn't show up anymore with the updated version above.
It crashes at the very end and the debugger reveals the following error: ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData InitString]: unrecognized selector sent to instance 0x6b1fe20'*
Furthermore, it says that "NewFreeze" is not of type CFString. Does anybody have a clue what's going on? I really want to save my Objects like that.