I am quite new to objective-c and got stuck with a quite simple task I assume. I use Xcode 4.0.1 and my deployment target is OS X 10.6.
I save and load some plain C ints s and struct values by using encodeWithCoder and initWithCoder.
This works quite good - the saved values are loaded and I can NSLog them in the initWithCoder method.
But when I want to use these loaded values in an other method, they are all gone. But I can not see why or where the values are lost.
Here are the relevant parts of my code:
My Header file:
@interface OptimiererZwoAppDelegate : NSDocument <NSApplicationDelegate,NSCoding> {
//Winkel in Grad
__strong struct bildanalyse {
float winkelo;
float winkelr;
... here are some more variables…
float skalfak;
};
__strong struct bildanalyse *analyse;
__strong int16_t anzahlanalysewerte;
...some more stuff....
}
...
- (NSData *) dataOfType:(NSString *)aType error:(NSError **)outError;
- (BOOL) readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError;
- (void) encodeWithCoder: (NSCoder *) coder;
- (id) initWithCoder: (NSCoder *) coder;
...
@property __strong struct bildanalyse *analyse;
@property __strong int16_t anzahlanalysewerte;
...
And this is my implementation file:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
...some code...
[self prepareApp];
}
-(void) prepareApp{
NSLog(@"prepareApp");
self.analyse = NSAllocateCollectable(sizeof(struct bildanalyse) * 256, 0);
self.anzahlanalysewerte = 0;
}
- (void) encodeWithCoder: (NSCoder *) coder{
[coder encodeInt:self.anzahlanalysewerte forKey: @"anzahlanalysewerte"];
for(int i=0; i< self.anzahlanalysewerte; i++){
[coder encodeFloat:self.analyse[i].winkelo forKey: [NSString stringWithFormat:@"winkelo%d",i]];
[coder encodeFloat:self.analyse[i].winkelr forKey: [NSString stringWithFormat:@"winkelr%d",i]];
...
[coder encodeFloat:self.analyse[i].skalfak forKey: [NSString stringWithFormat:@"skalfak%d",i]];
}
}
- (id) initWithCoder: (NSCoder *) coder{
self = [super init];
[self prepareApp];
self.anzahlanalysewerte = [coder decodeIntForKey:@"anzahlanalysewerte"];
for(int i=0; i< self.anzahlanalysewerte; i++){
self.analyse[i].winkelo = [coder decodeFloatForKey: [NSString stringWithFormat:@"winkelo%d",i]];
self.analyse[i].winkelr = [coder decodeFloatForKey: [NSString stringWithFormat:@"winkelr%d",i]];
...
self.analyse[i].skalfak = [coder decodeFloatForKey: [NSString stringWithFormat:@"skalfak%d",i]];
}
return self;
}