美文网首页
runtime之动态添加方法的补充

runtime之动态添加方法的补充

作者: Alexander | 来源:发表于2016-03-08 21:15 被阅读98次

前言

  • 1, 上一章简单讲述了runtime动态添加方法,但是都是没有参数的方法,下面我们学习一下带参数的方法.

runtime动态添加方法的补充

  • 应用场景 : 微博的会员机制
在ViewController.m文件中
#import "ViewController.h"
#import "WGStudent.h"
#import <objc/message.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    WGStudent *student = [[WGStudent alloc] init];

    [student performSelector:@selector(study:) withObject:@"在简书上学习iOS"];

}
@end
  • 注意 : 既然是有参数,那么冒号一定要记得写
在WGStudent.m文件中

#import "WGStudent.h"
#import <objc/message.h>

@implementation WGStudent

// 返回值Void-> V id -> @ SEL -> : NSString -> @
// v@:@
void study(id self, SEL _cmd, NSString *jianshu)
{

    NSLog(@"Alex%@",jianshu);

}

+ (BOOL)resolveInstanceMethod:(SEL)sel
{
    if (sel == NSSelectorFromString(@"study:")) {

        class_addMethod(self, sel, (IMP)study, "v@:@");

        return YES;
    }

  return [super resolveInstanceMethod:sel];
}

@end
  • 注意 : type是如何书写的(直接去查阅官方文档)

相关文章

  • runtime之动态添加方法的补充

    前言 1, 上一章简单讲述了runtime动态添加方法,但是都是没有参数的方法,下面我们学习一下带参数的方法. r...

  • runtime的理解(二)

    主要内容 利用 runtime 交换方法 利用 runtime 动态添加方法 利用 runtime 动态添加属性 ...

  • Runtime的动态添加方法

    Runtime动态添加方法,保持积累,有待后续补充具体的实现价值和例子

  • runtime

    runtime交换方法 动态添加方法

  • runtime和oc内存区域(2018-04-02)

    runtime常用的几个方法: 交换方法 动态添加属性 动态添加方法 1.交换方法 class_getClassM...

  • 自己实现OC的KVO

    Runtime系列文章在这:Runtime介绍---术语介绍Runtime应用--动态添加方法Runtime应用-...

  • IOSRunTime_动态添加方法

    利用RunTime运行时,动态添加一个方法 动态添加方法,首先实现这个resolveInstanceMethod ...

  • runtime简单使用之动态添加方法

    一, runtime的动态添加方法功能 1, 关于runtime动态添加方法,我们以一个经典的面试题展开对其的研究...

  • runTime之--动态添加方法

    Runtime运行时之--动态添加方法 - OC都是懒加载机制、只要方法实现了,就会马上添加到方法列表List中-...

  • runtime相关

    修改系统方法 动态添加方法 动态给系统类添加属性(给分类添加属性) runtime+kvc 转换模型

网友评论

      本文标题:runtime之动态添加方法的补充

      本文链接:https://www.haomeiwen.com/subject/knpzkttx.html