私は目的-cにはかなり新しいです。私はXcode 4.0.1を使用し、展開ターゲットはOS X 10.6です。initWithCoderの値(int、struct-values)がこのメソッドの外で失われています
encodeWithCoder
とinitWithCoder
を使用して、C ints
と構造体の値を保存して読み込みます。 これは非常にうまくいく - 保存された値がロードされ、NSLog
initWithCoder
メソッドでそれらができる。
しかし、これらの読み込まれた値を他の方法で使用したい場合、それらはすべて消えてしまいます。しかし、私はなぜ値が失われているのか分からない。ここで
は、私のコードの関連部分です:私のヘッダファイル:
@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)encodeWithCoder:(NSCoder *)encoder
実装が
- (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;
}
[OK]を - この情報をありがとうございました。私はそれを知りませんでした:-) – Arno
私はencodeWithCoder implementaionを投稿しませんでした。なぜならこの問題はこのメソッドにはないと思うからです。それがあれば、私はinitWithCoderで正しい値を見ることができません。 しかし私はそれを私の質問に今追加しました。 – Arno
ありがとうございました。私のアプリケーションの主な問題は、マルチドキュメントアーキテクチャを正しい方法で実装しなかったことです。これは、さまざまなウィンドウインスタンスの誤った取り扱いにつながります。 私はこれを修正しましたが、initWithCoderでインスタンス変数を読み取った後でも、インスタンス変数の値はまだ緩いです。しかし、私は今の状況がまったく異なっていると思います。私はこれを新しい質問に投稿します。 あなたのすべてのお供にもう一度感謝します! – Arno