我有一個對象,我正常分配/ init像獲得一個實例。後來在我的應用程序中,我想從磁盤加載該對象的狀態。我想我可以解開我的課程(符合NSCoding),並且只是在我的實例指向的地方交換。爲此我使用此代碼...改變自己的指針
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];
}
}
現在,當我撒一些NSLogs在我的應用我注意到
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)
我缺少什麼?
這兩個優秀的答案。這個給了我更多的背景,爲什麼我的方法是錯誤的,謝謝! – rob5408 2010-05-10 20:34:46