iOS开发之 运行时封装

背景

最近在做项目的时候用到了运行时,感觉他的代码很分散,于是写了一个工具RuntimeTool,把运行时常用的一些操作封装成了API,欢迎大家使用,如果感觉不错请记得start。

功能:

  • 获取类所有的变量列表
  • 获取类所有的属性列表
  • 获取类所有的方法列表
  • 通过NSInvocation调用方法
  • 运行时动态创建类
  • 运行时动态创建方法
  • 关联对象(关联,获取,删除)
  • 方法替换
  • 设置私有变量

Installation

RuntimeTool支持多种安装方法

installation with CocoaPods

RuntimeTool is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "RuntimeTool",'~>1.0'

然后,运行下面的命令:

pod install

Usage

获取所有变量列表

    [RuntimeTool getVarsForClassName:@"UIView"];
    [RuntimeTool getVarsForClass:[UIView class]];

获取所有的属性列表

    [RuntimeTool getPropertiesForClass:[UIView class]];
    [RuntimeTool getPropertiesForClassName:@"UIView"];

获取所有的方法列表

    [RuntimeTool getMethodsForClass:[UIView class]];
    [RuntimeTool getMethodsForClassName:@"UIView"];

通过NSInvocation调用方法

    TestClass * test = [TestClass new];
    id returnValue = [RuntimeTool invokeMethod:test method:@selector(setNum:height:name:) argumentValue:@(10), nil];

运行时动态创建类

    Class class = [RuntimeTool createClassOnRuntime:@"MyButton" superClass:@"UIButton"];

运行时动态创建方法

    NSString* className = @"MyButton";
    NSString* methodStr = @"printLog";
    Class newClass = [RuntimeTool createClassOnRuntime:className superClass:@"UIButton"];
    [RuntimeTool createMethodForClass:className methodStr:methodStr imp:(IMP)printLog];
    id instance = [[newClass alloc]init];
    [instance performSelector:NSSelectorFromString(methodStr) withObject:nil];
    
    //方法的实现
    void printLog(id self,SEL _cmd) {
    printf("hello,world");
}

关联对象

    //给当前controller关联一个color对象
    NSString* key = @"colorKey";
    [RuntimeTool createAssociatedObject:self key:key value:[UIColor yellowColor] policy:OBJC_ASSOCIATION_RETAIN];
    //获取color对象并设置背景色
    UIColor* color = (UIColor*)[RuntimeTool getAssociatedObjectValue:self key:key];
    self.view.backgroundColor = color;
    //删除关联对象
    [RuntimeTool removeAssociatedObjec:self];
    [self performSelector:@selector(clearColor) withObject:nil afterDelay:2];

方法替换

- (void)test1 {
    NSLog(@"test1");
}

- (void)test2 {
    NSLog(@"test2");
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"hello,wrold");
}

- (void)swizzle_viewWillAppear:(BOOL)animated{
    NSLog(@"world,hello");
}


//方式1(两个方法相互替换实现):
[self test1];
[RuntimeTool methodExchange:[self class] sel1:@selector(test1) sel2:@selector(test2)];
[self test2];
//输出结果都是  test1

//方式2(替换某个方法的实现):
[self viewWillAppear:NO];
[RuntimeTool swizzleMethodImp:[self class] sel:@selector(viewWillAppear:) imp:method_getImplementation(class_getInstanceMethod([self class], @selector(swizzle_viewWillAppear:)))];
[self viewWillAppear:NO];

//输出结果  hello,world    world,hello

设置私有变量

    TestClass * test = [TestClass new];
    test.num = @(100);
    NSLog(@"alter before: %@",test.num);
    [RuntimeTool setVarForObj:test key:@"_num" value:@(100000)];
    NSLog(@"alter after: %@",test.num);

Author

guodongyangw@163.com, guodongyang@qfpay.com

License

RuntimeTool is available under the MIT license. See the LICENSE file for more info.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 目录 Objective-C Runtime到底是什么 Objective-C的元素认知 Runtime详解 应用...
    Ryan___阅读 5,931评论 1 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,073评论 19 139
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,879评论 0 9
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,024评论 18 399
  • 某月某日的苏州车站, 某人和某人听见哭泣, 一群小金鱼惊慌流泪, 主人你何忍将我抛弃。 他们把它们轻轻捧起: 小鱼...
    斌姬阅读 1,315评论 0 1

友情链接更多精彩内容