Runtime 요약

4672 단어
  • runtime에서 하나의 대상은 구조체로 표시된다
  • /// An opaque type that represents a method in a class definition.  
    typedef struct objc_method *Method;
    
    /// An opaque type that represents an instance variable.  
    typedef struct objc_ivar *Ivar;
    
    /// An opaque type that represents a category.  
    typedef struct objc_category *Category;
    
    /// An opaque type that represents an Objective-C declared property.  
    typedef struct objc_property *objc_property_t;
    
    
  • runtime의 표시
  • struct objc_class {
        Class isa ;
    
    #if !__OBJC2__
        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 ;  //                   
    #endif
    
    } OBJC2_UNAVAILABLE;
    
  • 클래스의 속성 목록 가져오기
  • unsigned int count;
        objc_property_t *propertyList = class_copyPropertyList([self class], &count);
        for (unsigned int i = 0; i < count; i++) {
            const char *propertyName = property_getName(propertyList[i]);
            NSLog(@"proprety--->%@",[NSString stringWithUTF8String:propertyName]);
        }
    
  • 클래스의 구성원 변수 획득
  • unsigned int count;
    // 
    Ivar *ivarList = class_copyIvarList([self class], &count);
    for (unsigned int i; i%@", [NSString stringWithUTF8String:ivarname]);
    }
    
  • 클래스를 획득하는 모든 방법
  • // self 
    - (void)getMethods
    {
        unsigned int count;
        Method *methodlist = class_copyMethodList([self class], &count);
        for (int i = 0; i < count; i ++) {
            Method method = methodlist[i];
            NSLog(@"method---> %@",NSStringFromSelector(method_getName(method)));
        }
    }
    
  • 클래스가 준수하는 프로토콜 가져오기
  • // self 
    - (void)getProtocals
    {
        unsigned int count;
        __unsafe_unretained Protocol **protocolLsit = class_copyProtocolList([self class], &count);
        for (int i = 0; i < count; i++) {
            Protocol *protocol = protocolLsit[i];
            const char  *protoalName = protocol_getName(protocol);
            NSLog(@"protocol---> %@",[NSString stringWithUTF8String:protoalName]);
        }
    }
    
  • category에 속성을 추가할 수 있음
  • // key
    static char objcKey; 
    // 
    - (void)setObjcName:(NSString *)objcName
    {
        objc_setAssociatedObject(self, &objcKey, objcName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
       
    }
    - (NSString *)objcName
    {
        return  objc_getAssociatedObject(self, &objcKey);;
    }
    
  • 인쇄 결과
  • 2016-03-31 18:19:34.350 RumtimeModel[9164:325075] ivar--->_girl0
    2016-03-31 18:19:34.350 RumtimeModel[9164:325075] ivar--->_girl1
    2016-03-31 18:19:34.351 RumtimeModel[9164:325075] ivar--->_girl2
    2016-03-31 18:19:34.351 RumtimeModel[9164:325075] ivar--->_girl3
    2016-03-31 18:19:34.352 RumtimeModel[9164:325075] proprety--->girl0
    2016-03-31 18:19:34.353 RumtimeModel[9164:325075] proprety--->girl1
    2016-03-31 18:19:34.353 RumtimeModel[9164:325075] proprety--->girl2
    2016-03-31 18:19:34.353 RumtimeModel[9164:325075] proprety--->girl3
    2016-03-31 18:19:34.388 RumtimeModel[9164:325075] method---> setDicData:
    2016-03-31 18:19:34.389 RumtimeModel[9164:325075] method---> dicData
    2016-03-31 18:19:34.389 RumtimeModel[9164:325075] method---> getMethods
    2016-03-31 18:19:34.389 RumtimeModel[9164:325075] method---> getProtocals
    2016-03-31 18:19:34.389 RumtimeModel[9164:325075] method---> categotyString
    2016-03-31 18:19:34.390 RumtimeModel[9164:325075] method---> .cxx_destruct
    2016-03-31 18:19:34.390 RumtimeModel[9164:325075] method---> tableView:numberOfRowsInSection:
    2016-03-31 18:19:34.390 RumtimeModel[9164:325075] method---> tableView:cellForRowAtIndexPath:
    2016-03-31 18:19:34.390 RumtimeModel[9164:325075] method---> numberOfSectionsInTableView:
    2016-03-31 18:19:34.391 RumtimeModel[9164:325075] method---> didReceiveMemoryWarning
    2016-03-31 18:19:34.391 RumtimeModel[9164:325075] method---> viewDidLoad
    2016-03-31 18:19:34.391 RumtimeModel[9164:325075] protocol---> UITableViewDataSource
    2016-03-31 18:19:34.391 RumtimeModel[9164:325075] protocol---> UITableViewDelegate
    2016-03-31 18:19:34.392 RumtimeModel[9164:325075] category----> HHLM
    

    좋은 웹페이지 즐겨찾기