シングルトン自体でロードを行う必要があります。ここで行われているのは、シングルを作成し、lval をシングルトンに割り当て、次に新しいオブジェクトを作成し、シングルトンを変更せずにその新しいオブジェクトに lval を再割り当てすることです。言い換えると:
//Set venue to point to singleton
Venue *venue = [Venue sharedVenue];
//Set venue2 to point to singleton
Venue *venue2 = [Venue sharedVenue];
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//Set venue to unarchived object (does not change the singleton or venue2)
venue = [unarchiver decodeObjectForKey:@"Venue"];
[unarchiver finishDecoding];
あなたがしたいことは、sharedVenue でこれを処理することです。人々がシングルトンを行う方法はいくつかあるため、あなたが何をしているのかはわかりませんが、sharedVenue が現在次のようになっていると仮定しましょう。
static Venue *gSharedVenue = nil;
- (Venue *) sharedVenue {
if (!gSharedVenue) {
gSharedVenue = [[Venue alloc] init];
}
return gSharedVenue;
}
それを変更して、シングルトンをサポートするグローバルにオブジェクトをロードする場合を想定します。
static Venue *gSharedVenue = nil;
- (Venue *) sharedVenue {
if (!gSharedVenue) {
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[data release];
gSharedVenue = [unarchiver decodeObjectForKey:@"Venue"];
[unarchiver finishDecoding];
[unarchiver release];
}
if (!gSharedVenue) {
gSharedVenue = [[Venue alloc] init];
}
return gSharedVenue;
}
明らかに、アーカイブされたオブジェクト ファイルへの実際のパスを何らかの方法で伝える必要があります。
コメントに基づいて編集:
alloc ベースのシングルトンを使用している場合は、クラスの init メソッドでこれを処理する必要があります。
- (id) init {
self = [super init];
if (self) {
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[data release];
Venue *storedVenue = [unarchiver decodeObjectForKey:@"Venue"];
[unarchiver finishDecoding];
[unarchiver release];
if (storeVenue) {
[self release];
self = [storedVenue retain];
}
}
return self;
}