runtime-API-class

1.获取类名

    //1. const char * class_getName(Class cls)
    const char * className = class_getName(NSPerson.class);
    NSLog(@"class name : %s",className);

2.判断是否元类

    //2.
//    BOOL isMetaClass = class_isMetaClass(NSPerson.class);
    BOOL isMetaClass = class_isMetaClass(object_getClass(NSPerson.class));
    NSLog(@"isMetaClass : %d",isMetaClass);

3.获取父类

    Class superClass = class_getSuperclass(NSPerson.class);
    NSLog(@"%@",superClass);

4.设置父类(DEPRECATED)

    /**
     __OSX_DEPRECATED(10.5, 10.5, "not recommended")
     __IOS_DEPRECATED(2.0, 2.0, "not recommended")
     __TVOS_DEPRECATED(9.0, 9.0, "not recommended")
     __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")

     */
    
//    class_setSuperclass(Class  _Nonnull __unsafe_unretained cls, Class  _Nonnull __unsafe_unretained newSuper)
    

5.class version

/**@note You can use the version number of the class definition to provide versioning of the
interface that your class represents to other classes. This is especially useful for object
serialization (that is, archiving of the object in a flattened form), where it is important to
recognize changes to the layout of the instance variables in different class-definition versions.
*/

  class_setVersion(NSPerson.class, 1);
    
   NSLog(@"class_version : %d", class_getVersion(NSPerson.class));

6.获取类的实例对象的大小

    size_t size = class_getInstanceSize(NSPerson.class);
//    size_t size = class_getInstanceSize(object_getClass(NSPerson.class));

    NSLog(@"%zd",size);

    

8.成员变量(ivar)获取

  • class_getInstanceVariable
  • class_getClassVariable
  • class_copyIvarList
    Ivar age_ivar = class_getInstanceVariable(NSPerson.class,"_age");

    const char * age_name = ivar_getName(age_ivar);
    const char * age_type = ivar_getTypeEncoding(age_ivar);
    NSLog(@"name:%s , type: %s",age_name ,age_type);
    

    Ivar isa = class_getClassVariable(NSPerson.class,"isa");
    
    const char * isa_name = ivar_getName(isa);
    const char * isa_type = ivar_getTypeEncoding(isa);
    NSLog(@"name:%s , type: %s",isa_name ,isa_type);
    
    
    //8.class_copyIvarList
    unsigned int outCount = 0;
    Ivar *ivars = class_copyIvarList(NSPerson.class, &outCount);
    for (int i = 0; i < outCount; i++) {
        const char *name = ivar_getName(ivars[i]);
        NSLog(@"%s",name);
    }
    free(ivars);
  

9.方法(method)的获取

  • class_getInstanceMethod
  • class_getClassMethod
  • class_getMethodImplementation
  • class_respondsToSelector
  • class_copyMethodList
    Method method = class_getInstanceMethod(NSPerson.class, sel_registerName("run"));
    NSLog(@"%@",NSStringFromSelector(method_getName(method)));
    NSLog(@"%s",method_getTypeEncoding(method));
    NSLog(@"%p",method_getImplementation(method));
    
    Method clsMethod = class_getClassMethod(NSPerson.class, @selector(person));
    NSLog(@"%@",NSStringFromSelector(method_getName(clsMethod)));
    NSLog(@"%s",method_getTypeEncoding(clsMethod));
    NSLog(@"%p",method_getImplementation(clsMethod));
    
    
    IMP runIMP = class_getMethodImplementation(NSPerson.class, @selector(run));
    NSLog(@"%p",runIMP);
    
//    OBJC_ARM64_UNAVAILABLE
//    class_getMethodImplementation_stret(Class  _Nullable __unsafe_unretained cls, SEL  _Nonnull name)

//    区别都调用class_respondsToSelector_inst(id inst, SEL sel, Class cls),
    //inst 传nil
    BOOL isRun = class_respondsToSelector(NSPerson.class, @selector(run));
    NSLog(@"isRun:%d",isRun);
    
    //inst传self
    BOOL isR = [NSPerson.new respondsToSelector:@selector(run)];
    NSLog(@"isR:%d",isR);
   
    //12.class_copyMethodList
    unsigned int methodCount = 0;
    Method *methods = class_copyMethodList(object_getClass(NSPerson.class), &methodCount);
    for (int i = 0; i < methodCount; i++) {
        SEL mname = method_getName(*(methods + i));
        NSLog(@"+m%d:%@",i,NSStringFromSelector(mname));
    }
    free(methods);
    
    methods = class_copyMethodList(NSPerson.class, &methodCount);
    for (int i = 0; i < methodCount; i++) {
        SEL mname = method_getName(*(methods + i));
        NSLog(@"-m%d:%@",i,NSStringFromSelector(mname));
    }
    free(methods);

pragma mark - protocol

10.协议(protocol)的获取

  • class_conformsToProtocol
  • class_copyProtocolList
    BOOL isConformProtocol = class_conformsToProtocol(NSPerson.class,@protocol(NSPersonDelegate));
    
    //conformsToProtocol:遍历一个实例的类及其类的superclass然后 调用了class_conformsToProtocol,
//    BOOL isConformProtocol = [NSPerson.new conformsToProtocol:@protocol(NSPersonDelegate)];
    NSLog(@"isConformProtocol:%d",isConformProtocol);
    NSString *persond = NSStringFromProtocol(@protocol(NSPersonDelegate));
    NSLog(@"persond:%@",persond);
    
    
 unsigned int protocolCount = 0;
    Protocol * __unsafe_unretained * protocols = class_copyProtocolList(NSPerson.class, &protocolCount);
    
    for (int i = 0; i < protocolCount; i++) {
        NSLog(@"prtocol%i:%s",i,protocol_getName(*(protocols + i)));
    }
    free(protocols);
    

11.属性的获取

  • class_getProperty
  • class_copyPropertyList
    //T@"NSString",R,C,N,GgetName,V_name
    objc_property_t age_property = class_getProperty(NSPerson.class, "name");
    NSLog(@"%s,attr:%s",property_getName(age_property),property_getAttributes(age_property));
    
    unsigned int property_count = 0;
    objc_property_t *properties = class_copyPropertyList(NSPerson.class, &property_count);
    for (int i = 0; i < property_count; i++) {
        NSLog(@"property%i:%s",i,property_getName(*(properties + i)));
    }
    free(properties);
    

12.成员变量布局

  • class_getIvarLayout
  • class_getWeakIvarLayout
    

    const  uint8_t *ivar_layout = class_getIvarLayout(NSPerson.class);
    NSLog(@"ivar_layout:%s",ivar_layout);

    if(ivar_layout != NULL)
    {
        int i = 0;
        uint8_t value_s = ivar_layout[i];
        while (value_s != 0x0) {
            printf("ivar_layout:\\x%02x\n", value_s);
            value_s = ivar_layout[++i];
        }
    }
    
    const uint8_t *ivar_weak_layout = class_getWeakIvarLayout(NSPerson.class);

    if(ivar_weak_layout != NULL)
    {
        int i = 0;
        uint8_t value_s = ivar_weak_layout[i];
        while (value_s != 0x0) {
            printf("ivar_weak_layout:\\x%02x\n", value_s);
            value_s = ivar_weak_layout[++i];
        }

    }

12.向类中添加成员变量/方法/协议/属性

    //12.1.1.method add
    BOOL isAdd = class_addMethod(NSPerson.class, @selector(travel), (IMP)travel_add, "v@0:8");
    NSLog(@"isAdd:%d",isAdd);
    
    [NSPerson.new travel];
//    [NSPerson.new performSelector:@selector(travel)];
    
    
    NSLog(@"eatIMP1:%p",class_getMethodImplementation(NSPerson.class, @selector(eat)));
    
    //12.1.2.method replace
    //返回替换前方法的地址
    IMP methodIMP = class_replaceMethod(NSPerson.class, @selector(eat), (IMP)eat_replace,
                        "v@0:8");
    NSLog(@"eatIMP2:%p",class_getMethodImplementation(NSPerson.class, @selector(eat)));

    NSLog(@"methodIMP:%p",methodIMP);
    
    [NSPerson.new eat];
    
    //12.2.ivar
    Class NSCat = objc_allocateClassPair(NSObject.class, "NSCat", 0);
    
    BOOL isAddIvar = class_addIvar(NSCat, "_name", 8, 1<<0, @encode(NSString *));
    
    NSLog(@"isAddIvar:%d",isAddIvar);
    objc_registerClassPair(NSCat);
    
    
    //12.3.protocol
    BOOL is_add_protocol = class_addProtocol(NSPerson.class, @protocol(NSCopying));
    NSLog(@"is_add_protocol%d",is_add_protocol);
    
    //12.4.1.property add
    
    objc_property_attribute_t types = { "T", "@\"NSString\"" };

    objc_property_attribute_t ownership = {"C", ""}; // C = copy
//    objc_property_attribute_t ownership = {"&", ""}; // & = strong/retain

    objc_property_attribute_t atomic = {"N", ""}; // N = nonatomic

    objc_property_attribute_t rw = {"R", ""}; // R = readonly

    objc_property_attribute_t backIvar = {"V","_car"};

    objc_property_attribute_t attrs[] = { atomic,rw,types, ownership, backIvar };


    BOOL is_add_property = class_addProperty(NSPerson.class, "car", attrs, 5);
    
    NSLog(@"is_add_property:%d",is_add_property);
    
    //12.4.2.property replace
    objc_property_attribute_t age_t = {"T","@\"NSString\""};
    objc_property_attribute_t age_owner = {"C",""};
    objc_property_attribute_t age_atomic = {"N",""};
    objc_property_attribute_t age_rw = {"R",""};
    objc_property_attribute_t age_i = {"V","__age"};
    objc_property_attribute_t age_attr[] = {age_t,age_rw,age_owner,age_atomic,age_i};
    
    class_replaceProperty(NSPerson.class, "age", age_attr, 5);
    
//    unsigned int property_count = 0;
//    objc_property_t *properties = class_copyPropertyList(NSPerson.class, &property_count);
//    for (int i = 0; i < property_count; i++) {
//        NSLog(@"property%i:%s,%s",i,property_getName(*(properties + i)),property_getAttributes(*(properties + i)));
//    }
//    free(properties);

13.成员变量布局设置

#warning to learn

//    class_setIvarLayout(NSPerson.class, <#const uint8_t * _Nullable layout#>);
//    class_setWeakIvarLayout(NSPerson.class, <#const uint8_t * _Nullable layout#>)

14.创建实例

    NSPerson *person_0 = class_createInstance(NSPerson.class, 0);
    [person_0 travel];
    [person_0 run];
    [person_0 eat];

15.获取镜像(framework / dynamic library)

    const char * image = class_getImageName(NSPerson.class);
    NSLog(@"image:%s",image);
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容