runTime 基本用法
来源:互联网 发布:c语言定义变量的数组 编辑:程序博客网 时间:2024/05/29 16:12
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
- (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 *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
- runTime 基本用法
- iOS Runtime 几种基本用法简记
- runtime用法
- runTime用法
- iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
- Runtime类基本应用
- runtime基本使用
- Runtime基本使用
- Runtime.addShutdownHook()用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- RunTime.getRunTime().addShutdownHook用法
- VMware Ubuntu安装详细过程
- 什么是比特币51%攻击
- 简单的递推练习
- linux安装gcc、g++
- 依赖注入
- runTime 基本用法
- 关于C++编程的总结
- 第一次使用Android Studio时你应该知道的一切配置
- 如何添加Eclipse代码模板
- 牛客-剑指offer-树的子结构
- Java Object你真的了解了吗
- json转换工具
- 自定义类加载器[0]
- HRBUST1200 装修