이 애플의 아이폰 유틸리티 Aplication '템플릿 수정되지 않은 코드 :오브젝티브 C는 '자기'를 사용하면
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
}
mainViewController
이 aController
에 할당되면, self
키워드가 지정됩니다
self.mainViewController = aController;
mainViewController
의 프레임이 설정되어있는 경우
는 그러나 self
키워드가 필요하지 않습니다 :
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
내가 첫 번째 예에서
self
키워드를 제거하면
, 프로그램은 메시지와 함께 충돌 : 나는 두 번째 예제에 self
키워드를 추가하는 경우
objc[1296]: FREED(id): message view sent to freed object=0x3b122d0
이 프로그램이 잘 실행됩니다.
누군가 self
이 첫 번째 경우에는 필요하지만 두 번째 경우에는 필요하지 않은 이유를 설명 할 수 있습니까? 두 경우 모두 mainViewController
이 동일한 인스턴스 변수를 참조하고 있다고 가정합니다.
이 질문에 대한 답변 읽기 : http://stackoverflow.com/questions/2302891/self-instance-var-performance-hit – Felixyz