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