도대체

2295 단어
Objective-C 객체의 첫 번째 멤버 변수는 isa입니다.
먼저 공식적인 해석을 보고 개인적인 이해를 말씀드리겠습니다.Every object is connected to the run-time system through itsisa instance variable, inherited from the NSObject class.isa identifies the object's class; it points to a structurethat's compiled from the class definition. Through isa, anobject can find whatever information it needs at run timesuch asits place in the inheritance hierarchy, the size and structure ofits instance variables, and the location of the methodimplementations it can perform in response to messages.
하나의 대상 (Object) 의 이사는 이 대상의 클래스 (Class) 를 가리키고, 이 대상의 클래스 (Class) 의 이사는 Metaclass를 가리킨다.이렇게 하면 상응하는 정적 방법과 변수를 찾을 수 있다.Objective-C는 실행할 때 동적입니다. 실행할 때 방법을 추가하거나 삭제하거나 반사를 사용할 수 있습니다.
클래스의 실례 대상의isa는 클래스를 가리킨다.클래스의isa는 이 클래스의metaclass를 가리킨다.클래스의 슈퍼class는 상위 클래스를 가리키며, 이 클래스가 루트라면 값은 NULL입니다.metaclass의isa는 루트 metaclass를 가리키고 이 metaclass가 루트 metaclass라면 자신을 가리킨다.metaclass의 슈퍼class는 부모 metaclass를 가리키고 이 metaclass가 루트 metaclass라면 이 metaclass에 대응하는 클래스를 가리킨다.
// objc.h 
#if !OBJC_TYPES_DEFINED
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;

/// Represents an instance of a class.
struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

/// A pointer to an instance of a class.
typedef struct objc_object *id;
#endif

// NSObject.h 
@interface NSObject  {
    Class isa  OBJC_ISA_AVAILABILITY;
}

//   objc_class 
struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class super_class                                        OBJC2_UNAVAILABLE;
    const char *name                                         OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    long info                                                OBJC2_UNAVAILABLE;
    long instance_size                                       OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;

좋은 웹페이지 즐겨찾기