runTime 基本用法

1 获取类名

- (void)fetchClassName {

    constchar* name = class_getName([FirstViewControllerclass]);

    NSString *className = [NSStringstringWithUTF8String:name];

    NSLog(@"className::%@",className);

}


2 获取一个类的变量名称与类型

- (void)fetchIvarList:(Class)cls {

    

    unsignedint count = 0;

    Ivar *ivarList =class_copyIvarList(cls, &count);

    for (unsignedint i = 0; i<count; i++) {

        constchar * name = ivar_getName(ivarList[i]);

        constchar * type = ivar_getTypeEncoding(ivarList[i]);

        

        NSLog(@"%@ %@",[NSStringstringWithUTF8String:name],[NSStringstringWithUTF8String:type]);

    }

    

   free(ivarList);

}

@property (nonatomic,copy) NSString * name;


打印结果:

[96793:9742178] _name @"NSString"


3 获取属性列表

- (void)fetchIvarProperty:(Class)cls {

    unsignedint count = 0;

    objc_property_t * properties =class_copyPropertyList(cls, &count);

    for (unsignedint i = 0; i<count; i++) {

        constchar * property = property_getName(properties[i]);

        NSLog(@"%@ ",[NSStringstringWithUTF8String:property]);

    }

    free(properties);

}

@property (nonatomic,assign) NSInteger age;

@property  ( nonatomic copy NSString  * name;

打印结果:

2017-03-06 15:56:07.735 runTime[98303:9751944] age 

2017-03-06 15:56:07.735 runTime[98303:9751944] name 


4 获取类的实例方法

- (void)fetchMethodList:(Class)cls {

    unsignedint count = 0;

    Method * method =class_copyMethodList(cls, &count);

    for (unsignedint i = 0; i<count; i++) {

        SEL  methodName =method_getName(method[i]);

        constchar * types = method_getTypeEncoding(method[i]);

        NSLog(@"%@",[NSStringstringWithUTF8String:methodName]);

        

        

        

    }

    free(method);

    

}

打印结果:

2017-03-06 16:07:19.242 runTime[174:9762872] addMethod

2017-03-06 16:07:19.242 runTime[174:9762872] fetchMethodList:

2017-03-06 16:07:19.242 runTime[174:9762872] test

2017-03-06 16:07:19.243 runTime[174:9762872] fetchClassName

2017-03-06 16:07:19.243 runTime[174:9762872] fetchIvarList:

2017-03-06 16:07:19.243 runTime[174:9762872] fetchIvarProperty:

2017-03-06 16:07:19.243 runTime[174:9762872] age

2017-03-06 16:07:19.244 runTime[174:9762872] setAge:

2017-03-06 16:07:19.244 runTime[174:9762872] .cxx_destruct

2017-03-06 16:07:19.244 runTime[174:9762872] name

2017-03-06 16:07:19.244 runTime[174:9762872] setName:

2017-03-06 16:07:19.245 runTime[174:9762872] didReceiveMemoryWarning

2017-03-06 16:07:19.245 runTime[174:9762872] viewDidLoad


5 获取协议列表

- (void)fetchProtocalList:(Class)cls {

    unsignedint count = 0;

  __unsafe_unretained Protocol * *protocols =class_copyProtocolList(cls, &count);

    for (unsignedint i = 0; i<count; i++) {

      constchar * name= protocol_getName(protocols[i]);

        NSLog(@"%@",[NSStringstringWithUTF8String:name]);

    }

    free(protocols);

}

 打印结果:

2017-03-06 16:15:47.492 runTime[1595:9771626] UIWebViewDelegate

2017-03-06 16:15:47.492 runTime[1595:9771626] UITextViewDelegate


6 动态的添加方法

- (void)addMethod {

    

    SEL myName =@selector(test);

    Method method =class_getInstanceMethod([selfclass], @selector(test));

    IMP methodIMP =method_getImplementation(method);

    constchar *types = method_getTypeEncoding(method);


    class_addMethod([FirstViewControllerclass], myName, methodIMP, types);

    

}


以上给 FirstViewController 类添加了一个方法,如下调用

 FirstViewController *vc=[[FirstViewControlleralloc]init];

 [vc performSelector:@selector(test)withObject:nil];



7 方法实现交换

这一点我在博客无码埋点说过,这是实现无码埋点的一个关键点


- (void)methodSwap:(Class)cls MethodOne:(SEL )methodOne MethodTwo:(SEL )methodTwo{


    Method  method1 =class_getInstanceMethod(cls, methodOne);

    Method  method2 =class_getInstanceMethod(cls, methodTwo);

    

    method_exchangeImplementations(method1, method2);

    

}

8 在类别中 为类动态的添加变量

@interface UIViewController (category)


@property(nonatomic,strong)PasswordTextField *passwordTextField;


@end


@implementation UIViewController (category)


-(PasswordTextField *)passwordTextField

{

  return  objc_getAssociatedObject(self,@selector(passwordTextField));

}


-(void)setPasswordTextField:(PasswordTextField *)value

{

    objc_setAssociatedObject(self,@selector(passwordTextField), value,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}


@end










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值