Changing pointer of self

Posted by rob5408 on Stack Overflow See other posts from Stack Overflow or by rob5408
Published on 2010-05-10T20:10:56Z Indexed on 2010/05/10 20:14 UTC
Read the original article Hit count: 190

I have an object that I alloc/init like normal just to get a instance. Later in my application I want to load state from disk for that object. I figure I could unarchive my class (which conforms to NSCoding) and just swap where my instance points to. To this end I use this code...

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];
    }
}

Now when I sprinkled some NSLogs throughout my application I noticed

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)

What am I missing?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa