인스턴스를 얻으려면 보통/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