f와 [[super allocWithZone : nil] init]을 호출합니다. f. 이자형. (단지 더 명확한 메시지 메커니즘을 이해하기위한) 나는 클래스메시징 메커니즘
에서 MyClass.h이
@interface MyClass : NSObject {
int ivar1;
int ivar2;
}
+ (id)instance;
@end
MyClass.m
[super allocWithZone:nil]
를 호출 할 때 실제로 objc_msgSend에 전송됩니다 무엇
static MyClass* volatile _sInstance = nil;
@implementation MyClass
+ (id)instance {
if (!_sInstance) {
@synchronized(self) {
if (!_sInstance) {
_sInstance = [[super allocWithZone:nil] init];
}
}
}
return _sInstance;
}
@end
?
objc_msgSend([MyClass class], "allocWithZone", nil)
또는 objc_msgSend([NSObject class], "allocWithZone", nil)
? 실제로
나는 objc_msgSend(self, "allocWithZone", nil)
라고 생각하고이 경우 self == [MyClass class];
에 나는 ivar1 동안 그 기억해야 할 및 ivar2이 할당됩니다.
클래스 메서드에서 super를 호출하면 objc_msgSend() 함수에서 "self"인수가 전달됩니다. 즉,이 경우에는 자식의 클래스 개체입니까? 그리고 allocWithZone은 자식 클래스 객체를 "보면서"ivar1과 ivar2에 할당되어야하는 메모리 양을 확인합니다.
감사합니다.
아마도 [[MyClass super] alloc]'을 호출하여'MyClass'의 인스턴스를 할당하겠습니까? – trojanfoe
@trojanfoe 그것은 "alloc"메쏘드가 될 수 있고, 그렇게 중요하지 않다.) – BergP
하지만 [super alloc] (또는 [super allocWithZone]')을 호출하는 것이 어떻게 추가 공간을 위해 충분한 공간을 할당하는지 이해할 수 없다. 'self'의 인스턴스 변수? – trojanfoe