J'ai un objet que j'all/init comme normal juste pour obtenir une instance. Plus tard dans mon application, je veux charger l'état du disque pour cet objet. Je pense que je pourrais désarchiver ma classe (ce qui est conforme à NSCoding) et juste échanger où mon instance pointe vers. A cette fin, j'utilise ce code ...Changer le pointeur de soi
NSString* pathForDataFile = [self pathForDataFile];
if([[NSFileManager defaultManager] fileExistsAtPath:pathForDataFile] == YES)
{
NSLog(@"Save file exists");
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:pathForDataFile];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[data release];
Person *tempPerson = [unarchiver decodeObjectForKey:@"Person"];
[unarchiver finishDecoding];
[unarchiver release];
if (tempPerson)
{
[self release];
self = [tempPerson retain];
}
}
Maintenant, quand je saupoudré quelques NSLogs tout au long de ma demande, j'ai remarqué
self.person: <Person: 0x3d01a10> (After I create the object with alloc/init)
self: <Person: 0x3d01a10> (At the start of this method)
tempPerson: <Person: 0x3b1b880> (When I create the tempPerson)
self: <Person: 0x3b1b880> (after i point self to the location of the tempPerson)
self.person: <Person: 0x3d01a10> (After the method back in the main program)
Qu'est-ce que je manque?
Les deux excellentes réponses. Celui-ci me donne un peu plus d'informations sur les raisons pour lesquelles mon approche était si mauvaise, merci! – rob5408