前言
- 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是如何书写的(直接去查阅官方文档)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。