runtime 常用方法实例

Runtime(也就是运行时),它基本上是用C和汇编写的,这个库使得C语言有了面向对象的能力。所以习惯了面向对象编程的小伙们好像对于C的API一般都不太友好,但是又有很多C的API强大到别人问你你不知道那你都抬不起头,Runtime就是其中之一。关于它的传说有很多,可以说在有的人手里牛逼的不可一世。下面我们主要看一下runtime基本使用的方法:

1、获取当前类变量列表

不啰嗦直接上代码:

  获取当前类 的所以变量
  unsigned int outCount;
    Ivar *ivars = class_copyIvarList([PeopleModel class], &outCount);
    SGTLog(@"\n\n\nPeopleModel类包含 变量数量:%u", outCount);
    
    for (int i = 0; i < outCount; i++) {
        Ivar ivar = ivars[i];
        const char *ivarName = ivar_getName(ivar);
        const char *ivarType = ivar_getTypeEncoding(ivar);
        SGTLog(@"PeopleModel类包含 变量名为:%s 变量类型为:%s", ivarName, ivarType);
    }
    //由于ARC只适用于Foundation等框架,对runtime 等并不适用,所以ivars需要free()手动释放。
    free(ivars);
    
    获取当前类 实例对象的所以变量
    unsigned int count_p;
    Ivar *ivars_p = class_copyIvarList([self.people class], &count_p);
    SGTLog(@"\n\n\nself.people实例包含 变量数量:%u", count_p);
    
    for (int i = 0; i < count_p; i++) {
        Ivar ivar_p = ivars_p[i];
        const char *ivarName_p = ivar_getName(ivar_p);
        const char *ivarType_p = ivar_getTypeEncoding(ivar_p);
        SGTLog(@"self.people实例包含 变量名为:%s 变量类型为:%s", ivarName_p, ivarType_p);
    }
    
    free(ivars_p);
    
    

打印结果

PeopleModel类包含 变量数量:7
2019-05-13 14:31:33.967546+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:78]
PeopleModel类包含 变量名为:_weight 变量类型为:f
2019-05-13 14:31:33.967704+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:78]
PeopleModel类包含 变量名为:_sex 变量类型为:B
2019-05-13 14:31:33.967838+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:78]
PeopleModel类包含 变量名为:_height 变量类型为:f
2019-05-13 14:31:33.967978+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:78]
PeopleModel类包含 变量名为:_age 变量类型为:q
2019-05-13 14:31:33.968102+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:78]
PeopleModel类包含 变量名为:_province 变量类型为:@"NSString"
2019-05-13 14:31:33.968225+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:78]
PeopleModel类包含 变量名为:_city 变量类型为:@"NSString"
2019-05-13 14:31:33.968349+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:78]
PeopleModel类包含 变量名为:_county 变量类型为:@"NSString"
2019-05-13 14:31:33.968561+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:85]


self.people实例包含 变量数量:7
2019-05-13 14:31:33.969885+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:91]
self.people实例包含 变量名为:_weight 变量类型为:f
2019-05-13 14:31:33.970044+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:91]
self.people实例包含 变量名为:_sex 变量类型为:B
2019-05-13 14:31:33.970163+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:91]
self.people实例包含 变量名为:_height 变量类型为:f
2019-05-13 14:31:33.970283+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:91]
self.people实例包含 变量名为:_age 变量类型为:q
2019-05-13 14:31:33.970397+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:91]
self.people实例包含 变量名为:_province 变量类型为:@"NSString"
2019-05-13 14:31:33.970513+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:91]
self.people实例包含 变量名为:_city 变量类型为:@"NSString"
2019-05-13 14:31:33.970630+0800 RuntimeDemo[19130:320593] 
-[ViewController testIvar] [ViewController.m  Line:91]
self.people实例包含 变量名为:_county 变量类型为:@"NSString"
2、获取当前类属性列表

代码如下:

 unsigned int outPropertyCount;
    objc_property_t *propertys = class_copyPropertyList([PeopleModel class],&outPropertyCount);
    SGTLog(@"\n\n\nPeopleModel类包含 属性数量:%u", outPropertyCount);
    for (int i = 0; i < outPropertyCount; i++) {
        objc_property_t property = propertys[i];
        const char *propertyName = property_getName(property);
        const char *propertyAttributes = property_getAttributes(property);
        SGTLog(@"nPeopleModel类包含 属性名为:%s 变量属性为:%s", propertyName, propertyAttributes);
    }
    free(propertys);
    

打印结果,People类共有8个属性

PeopleModel类包含 属性数量:8
2019-05-13 14:52:17.738854+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:fatherName 变量属性为:T@"NSString",C,D,N
2019-05-13 14:52:17.739009+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:motherName 变量属性为:T@"NSString",C,D,N
2019-05-13 14:52:17.739128+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:age 变量属性为:Tq,N,V_age
2019-05-13 14:52:17.739276+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:sex 变量属性为:TB,N,V_sex
2019-05-13 14:52:17.739404+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:height 变量属性为:Tf,N,V_height
2019-05-13 14:52:17.739541+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:province 变量属性为:T@"NSString",C,N,V_province
2019-05-13 14:52:17.739697+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:city 变量属性为:T@"NSString",C,N,V_city
2019-05-13 14:52:17.739849+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:109]
nPeopleModel类包含 属性名为:county 变量属性为:T@"NSString",C,N,V_county
2019-05-13 14:52:17.740080+0800 RuntimeDemo[19435:328966] 
-[ViewController testProperty] [ViewController.m  Line:116]

3、获取当前类方法列表

代码如下:

 unsigned int outMethodCount;
    Method *methods = class_copyMethodList([PeopleModel class], &outMethodCount);
    SGTLog(@"\n\n\nPeopleModel类包含 方法数量:%u", outMethodCount);
    for (int i = 0; i < outMethodCount; i++) {
        Method method = methods[i];
        SEL method_name = method_getName(method);//方法名
        const char * method_types  = method_getTypeEncoding(method);//方法类型编码
        IMP method_imp = method_getImplementation(method);//方法实现
        unsigned int ArgumentsNum  =  method_getNumberOfArguments(method);//参数个数
        SGTLog(@"PeopleModel类包含 方法 名称为:%@ 方法类型为:%s  method_imp %p 参数个数: %d ",NSStringFromSelector(method_name), method_types,&method_imp,ArgumentsNum);
    }
    free(methods);
    

打印结果显示,People共有方法22个包含属性的setter getter 方法,也包含分类中的方法

PeopleModel类包含 方法数量:22
2019-05-13 17:30:45.841292+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:fatherName 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.841507+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:motherName 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.841651+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:isEat 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.841807+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:isEat 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.841945+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:isWork 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.842094+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setFatherName: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.842221+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setMotherName: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.853483+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:isHeight: 方法类型为:B24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.853635+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:sex 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.853767+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setSex: 方法类型为:v20@0:8B16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.853892+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:province 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.854019+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setProvince: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.854145+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:county 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.854278+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setCounty: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.854402+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:.cxx_destruct 方法类型为:v16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.854522+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:dealloc 方法类型为:v16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.854748+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:height 方法类型为:f16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.855102+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setHeight: 方法类型为:v20@0:8f16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.855471+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setAge: 方法类型为:v24@0:8q16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.855768+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:age 方法类型为:q16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.856054+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:setCity: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
2019-05-13 17:30:45.856447+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:137]
PeopleModel类包含 方法 名称为:city 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
2019-05-13 17:30:45.856783+0800 RuntimeDemo[25078:406610] 
-[ViewController testMethod] [ViewController.m  Line:146]
4、替换当前类实例方法的实现(类方法同样)

一样,不啰嗦直接上代码

 /// 替换方法实现
    SEL originalSelector = @selector(isEat);
    SEL swizzledSelector = @selector(isWork);
    
//    Method originalMethod = class_getInstanceMethod([PeopleModel class], originalSelector);
//    Method swizzledMethod = class_getInstanceMethod([PeopleModel class], swizzledSelector);
   
    //self.people 类型已被赋值为Model
    Method originalMethod = class_getInstanceMethod([self.people class], originalSelector);
    Method swizzledMethod = class_getInstanceMethod([self.people class], swizzledSelector);
//    if (swizzledMethod == nil) {
//        return;
//    }
    
    //1、交换两个方法的实现
    method_exchangeImplementations(originalMethod, swizzledMethod);
    
//    IMP    originalIMP    = method_getImplementation(originalMethod);
//    IMP    swizzledIMP    = method_getImplementation(swizzledMethod);
//    //2、用swizzledMethod的实现取代originalMethod的实现
//    class_replaceMethod([self.people class], originalSelector, swizzledIMP, method_getTypeEncoding(swizzledMethod));
//
//    //3、把swizzledVCMethod方法的实现赋值给originalMethod
//    method_setImplementation(originalMethod,swizzledIMP);
 
    //调用方法测试 //如果前边调用了[self textClass]方法 self.people 类型已被赋值为Model ,相当于调用Model 实例的isEat方法,Model 未实现isEat方法,Model类作了消息截取,可以调用成功
    [self.people isEat];

替换方法的实现有三种方法

1、交换两个方法的实现
method_exchangeImplementations(originalMethod,swizzledMethod);

2、用swizzledMethod的实现取代originalMethod的实现
class_replaceMethod([self.people class], originalSelector,method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

3、把swizzledVCMethod方法的实现赋值给originalMethod
method_setImplementation(originalMethod,method_getImplementation(swizzledMethod));

这三种方法都可以在调用originalMethod 的时候替换为swizzledMethod的实现

我们在项目中调用没有实现的方法会崩溃,怎么避免呢?我们可以通过消息转发来实现,代码如下,
并且重写消息转发的方法:
方法一:

//Model.m 文件中创建
-(void)noObjMethod{
    NSLog(@"未实现这个实例方法");
}
+(void)noClassMethod{
    NSLog(@"未实现这个类方法");
}
//并且重写消息转发的方法:
// 当一个对象调用未实现的方法,会调用这个方法处理,并且会把对应的方法列表传过来.
//注意:实例方法是存在于当前对象对应的类的方法列表中
+(BOOL)resolveInstanceMethod:(SEL)sel{
    SEL aSel = NSSelectorFromString(@"noObjMethod");
    Method aMethod = class_getInstanceMethod(self, aSel);
    BOOL addMethod = class_addMethod(self, sel, method_getImplementation(aMethod), "v@:");
    return addMethod;
}
// 当一个类调用未实现的方法,会调用这个方法处理,并且会把对应的方法列表传过来.
//注意:类方法是存在于类的元类的方法列表中
+(BOOL)resolveClassMethod:(SEL)sel{
    SEL aSel = NSSelectorFromString(@"noClassMethod");
    Method aMethod = class_getClassMethod(self, aSel);
    BOOL addMethod =  class_addMethod(object_getClass(self), sel, method_getImplementation(aMethod), "v@:");
    return addMethod;
}

打印结果

2019-05-13 18:31:46.605919+0800 RuntimeDemo[28185:448815] 未实现这个实例方法

方法二:

//消息转发
-(id)forwardingTargetForSelector:(SEL)aSelector{
    //获取people类
    Class peopleClass = NSClassFromString(@"PeopleModel");
    
    if (class_respondsToSelector(peopleClass, aSelector)) {
        return [[peopleClass alloc]init];
    }
    return self;
}

打印结果

2019-05-13 18:35:25.711990+0800 RuntimeDemo[28222:449877] 
-[PeopleModel(Family) isEat] [PeopleModel+Family.m  Line:22]
 (null) 没有吃饭

方法三:

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    if ([NSStringFromSelector(aSelector) isEqualToString:@"isEat"]) {
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }
    return [super methodSignatureForSelector:aSelector];
}
-(void)forwardInvocation:(NSInvocation *)anInvocation{
    [anInvocation invokeWithTarget:[[NSClassFromString(@"PeopleModel") alloc] init]];
}

打印结果

2019-05-13 18:40:26.972046+0800 RuntimeDemo[28102:446152] 
-[PeopleModel(Family) isEat] [PeopleModel+Family.m  Line:22]
 (null) 没有吃饭

以上三种方法都可以实现消息转发,在调用isEat时进行处理,不会引起崩溃。

Runtime使用实例就先到这里了。
测试Demo地址直通车

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

推荐阅读更多精彩内容