이모저모

objective c 클래스 객체 본문

iOS/etc

objective c 클래스 객체

Alpa 2017. 9. 1. 14:50

구현 구조


typedef struct objc_object{ 

Class isa; 

} *id;


typedef struct objc_selector  *SEL; 


typedef id (*IMP)(id self,SEL _cmd,...); 


struct objc_class {

    Class isa;

    Class super_class             

    const char *name              

    long version                    

    long info                          

    long instance_size    

    struct objc_ivar_list *ivars    

    struct objc_method_list **methodLists 

    struct objc_cache *cache  

    struct objc_protocol_list *protocols

};


typedef struct objc_class *Class;



Class 객체

- Class 구조체는 클래스의 메타데이터를 포함하는데

   클래스의 인스턴스가 구현한 method들과 instance 변수들에 대한 것이다.

- isa 포인터가 의미하는 것은 Class 자체가 객체임을 의미한다.

- Class 타입(즉 클래스 인스턴스의 isa 포인터)은 metaclass라는 또 다른 클래스이다.

- 오직 한개만 존재한다.

 

Metaclass

- 클래스 자체의 인스턴스에 대한 메타데이터를 표현하는데 사용

- Metaclass는 +method 즉 클래스 메서드가 정의되어 있는 곳이다.

- class 메소드는 class 객체의 인스턴스 메소드로 여겨지고 있다.

- 오직 한개만 존재한다.


introspection


클래스 계층도

isMemberOfClass, isKindOfClass를 이용해 객체가 특정 클래스 또는 상속 계층 클래스인지 알아 냄

introspection은 객체의 클래스를 얻기 위해 isa 포인터를 활용하고 super_class 포인터로 상속 계층을 따라 올라 갈 수 있기 때문이다.


클래스 객체를 직접 비교하기 보다는 introspection 메소드를 사용하는 것이 좋다.




'iOS > etc' 카테고리의 다른 글

c++ shard_ptr  (0) 2017.09.26
ReplayKit  (0) 2017.09.05
메시징  (0) 2017.09.01
property  (0) 2017.09.01
메모리 관리  (0) 2017.08.24
Comments