【发布时间】:2011-08-15 15:15:33
【问题描述】:
我对 Objective-c 很陌生,并且被我假设的一个非常简单的任务困住了。我使用 Xcode 4.0.1,我的部署目标是 OS X 10.6。
我使用encodeWithCoder 和initWithCoder 保存并加载了一些普通的C ints 和结构值。
这非常有效 - 保存的值已加载,我可以在 initWithCoder 方法中 NSLog 它们。
但是当我想在其他方法中使用这些加载的值时,它们都消失了。但我看不出价值丢失的原因或位置。
以下是我的代码的相关部分:
我的头文件:
@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;
...
这是我的实现文件:
- (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;
}
【问题讨论】:
标签: objective-c cocoa osx-snow-leopard