#import "ViewController.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <dlfcn.h>
#import <mach-o/ldsyms.h>
@interface ViewController ()<UITableViewDelegate>
{
NSString *_runtime1;
__weak id _lzID;
}
@property (nonatomic ,copy) NSString *lz;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - 解除IMP与Block关联
- (void)lz_imp_removeBlock {
BOOL result = imp_removeBlock(class_getMethodImplementation(self.class, NSSelectorFromString(@"lz_method")));
NSLog(@"lz_imp_removeBlock:%d",result);
}
#pragma mark - 通过IMP获取Block
- (void)lz_imp_getBlock {
void (^blcok) (NSString *) = ^(NSString *str) {
NSLog(@"lz_imp_implementationWithBlock:%@",str);
};
void (^result) (NSString *) = imp_getBlock(imp_implementationWithBlock(blcok));
result(@"hello word");
}
#pragma mark - 通过block获取一个方法的实现
- (void)lz_imp_implementationWithBlock {
void (^blcok) (NSString *) = ^(NSString *str) {
NSLog(@"lz_imp_implementationWithBlock");
};
IMP result = imp_implementationWithBlock(blcok);
result();
}
#pragma mark - 设置突变处理函数
- (void)lz_objc_setEnumerationMutationHandler {
objc_setEnumerationMutationHandler(enumerationMutationHandler);
}
void enumerationMutationHandler(id obj) {
NSLog(@"enumerationMutationHandler");
}
#pragma mark - 检测到突变时,编译器将插入次函数 并且调用objc_setEnumerationMutationHandler设置的回调函数,如果没有设置,则会崩溃
- (void)lz_objc_enumerationMutation {
objc_enumerationMutation(self);
}
#pragma mark - 获取属性的属性列表
- (void)lz_property_copyAttributeList {
objc_property_t t = class_getProperty(self.class, [@"lz" UTF8String]);
unsigned int count = 0;
objc_property_attribute_t *result = property_copyAttributeList(t, &count);
for (int i = 0; i < count; i ++) {
objc_property_attribute_t t = result[i];
NSLog(@"lz_property_copyAttributeList:%@",[NSString stringWithUTF8String:t.name]);
}
free(result);
}
#pragma mark - 获取属性的指定属性的值
- (void)lz_property_copyAttributeValue {
objc_property_t t = class_getProperty(self.class, [@"lz" UTF8String]);
char *result = property_copyAttributeValue(t, [@"T" UTF8String]);
NSLog(@"lz_property_copyAttributeValue:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取属性的属性
- (void)lz_property_getAttributes {
objc_property_t t = class_getProperty(self.class, [@"lz" UTF8String]);
const char *result = property_getAttributes(t);
NSLog(@"lz_property_getAttributes:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取属性的名称
- (void)lz_property_getName {
objc_property_t t = class_getProperty(self.class, [@"lz" UTF8String]);
const char *result = property_getName(t);
NSLog(@"lz_property_getName:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 判断一个协议是否遵循另一个协议
- (void)lz_protocol_conformsToProtocol {
Protocol *pro1 = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
Protocol *pro2 = objc_getProtocol([@"UIScrollViewDelegate" UTF8String]);
BOOL result = protocol_conformsToProtocol(pro1, pro2);
NSLog(@"lz_protocol_conformsToProtocol:%d",result);
}
#pragma mark - 获取协议中采用的协议列表
- (void)lz_protocol_copyProtocolList {
Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
unsigned int count = 0;
Protocol __unsafe_unretained **result = protocol_copyProtocolList(pro, &count);
for (int i = 0; i < count; i ++) {
Protocol *p = result[i];
NSLog(@"lz_protocol_copyProtocolList:%@",p);
}
free(result);
}
#pragma mark - 获取协议中指定的属性
- (void)lz_protocol_getProperty {
Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
objc_property_t result = protocol_getProperty(pro, [@"" UTF8String], YES, YES);
NSLog(@"lz_protocol_getProperty:%p",result);
}
#pragma mark - 获取协议的属性列表
- (void)lz_protocol_copyPropertyList {
Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
unsigned int count = 0;
objc_property_t *result = protocol_copyPropertyList(pro, &count);
for (int i = 0; i < count; i ++) {
objc_property_t t = result[i];
NSLog(@"lz_protocol_copyPropertyList:%p",t);
}
}
#pragma mark - 获取协议中指定方法的描述结构体
- (void)lz_protocol_getMethodDescription {
Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
struct objc_method_description result = protocol_getMethodDescription(pro, NSSelectorFromString(@"tableView:willDisplayCell:forRowAtIndexPath:"), NO, YES);
NSLog(@"lz_protocol_getMethodDescription:%p",result.name);
}
#pragma mark - 获取满足条件的协议的方法列表
- (void)lz_protocol_copyMethodDescriptionList {
Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
unsigned int count = 0;
struct objc_method_description *result = protocol_copyMethodDescriptionList(pro, NO, YES, &count);
for (int i = 0; i < count; i ++) {
struct objc_method_description des = result[i];
NSLog(@"lz_protocol_copyMethodDescriptionList:%p",des.name);
}
}
#pragma mark - 判断俩个协议是否相等
- (void)lz_protocol_isEqual {
Protocol *pro1 = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
Protocol *pro2 = objc_getProtocol([@"UITableViewDataSource" UTF8String]);
BOOL result = protocol_isEqual(pro1, pro2);
NSLog(@"lz_protocol_isEqual:%d",result);
}
#pragma mark - 获取协议的名称
- (void)lz_protocol_getName {
Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
const char *result = protocol_getName(pro);
NSLog(@"lz_protocol_getName:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 向协议中添加属性
- (void)lz_protocol_addProperty {
Protocol *pro = objc_allocateProtocol([@"LZProtocol" UTF8String]);
objc_property_attribute_t type = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership = { "C", "" };
objc_property_attribute_t backingivar = { "V", "_lz" };
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
protocol_addProperty(pro, [@"lz" UTF8String], attrs, 3, YES, YES);
}
#pragma mark - 向协议中添加协议
- (void)lz_protocol_addProtocol {
Protocol *pro1 = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
Protocol *pro2 = objc_allocateProtocol([@"LZProtocol" UTF8String]);
protocol_addProtocol(pro1, pro2);
}
#pragma mark - 向协议添加方法
- (void)lz_protocol_addMethodDescription {
Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
protocol_addMethodDescription(pro, NSSelectorFromString(@"lz_method"), [@"lzProtocolMethod" UTF8String], YES, YES);
}
#pragma mark - 注册协议
- (void)lz_objc_registerProtocol {
Protocol *result = objc_allocateProtocol([@"LZProtocol" UTF8String]);
objc_registerProtocol(result);
}
#pragma mark - 创建协议
- (void)lz_objc_allocateProtocol {
Protocol *result = objc_allocateProtocol([@"LZProtocol" UTF8String]);
NSLog(@"lz_objc_allocateProtocol:%@",result);
}
#pragma mark - 获取所有的协议列表
- (void)lz_objc_copyProtocolList {
unsigned int count = 0;
Protocol __unsafe_unretained **result = objc_copyProtocolList(&count);
for (int i = 0; i < count; i ++) {
Protocol *pro = result[i];
NSLog(@"lz_objc_copyProtocolList:%@",pro);
}
}
#pragma mark - 获取指定的协议
- (void)lz_objc_getProtocol {
Protocol *result = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
NSLog(@"lz_objc_getProtocol:%@",result);
}
#pragma mark - 判断俩个选择器是否相等
- (void)lz_sel_isEqual {
SEL sel1 = NSSelectorFromString(@"lz_method1");
SEL sel2 = NSSelectorFromString(@"lz_method2");
BOOL result = sel_isEqual(sel1, sel2);
NSLog(@"lz_sel_isEqual:%d",result);
}
#pragma mark - 注册方法
- (void)lz_sel_getUid {
SEL result = sel_getUid([@"lzlzlz" UTF8String]);
NSLog(@"sel_getUid:%p",result);
}
#pragma mark - 注册方法
- (void)lz_sel_registerName {
SEL result = sel_registerName([@"lzlzlz" UTF8String]);
NSLog(@"lz_sel_registerName:%p",result);
}
#pragma mark - 获取方法的名称
- (void)lz_sel_getName {
SEL sel = NSSelectorFromString(@"lz_method");
const char *result = sel_getName(sel);
NSLog(@"lz_sel_getName:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取指定库或框架中的所有类的名称
- (void)lz_objc_copyClassNamesForImage {
unsigned int outCount = 0;
Dl_info info;
dladdr(&_mh_execute_header, &info);
const char **result = objc_copyClassNamesForImage(info.dli_fname, &outCount);
for (int i = 0; i < outCount; i ++) {
const char *c = result[i];
NSLog(@"lz_objc_copyClassNamesForImage:%@",[NSString stringWithUTF8String:c]);
}
}
#pragma mark - 获取类源自的动态库的名称
- (void)lz_class_getImageName {
const char *result = class_getImageName(self.class);
NSLog(@"lz_class_getImageName:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取所有的objc框架和动态库
- (void)lz_objc_copyImageNames {
unsigned int a = 0;
const char **result = objc_copyImageNames(&a);
for (int i = 0; i < a; i ++) {
const char *c = result[i];
NSLog(@"lz_objc_copyImageNames:%@",[NSString stringWithUTF8String:c]);
}
}
#pragma mark - 交换俩种方法的实现
- (void)lz_method_exchangeImplementations {
Method method1 = class_getInstanceMethod(self.class, @selector(lz_method1));
Method method2 = class_getInstanceMethod(self.class, @selector(lz_method2));
method_exchangeImplementations(method1, method2);
[self lz_method1];
[self lz_method2];
}
#pragma mark - 设置方法的实现
- (void)lz_method_setImplementation {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
IMP imp = class_getMethodImplementation(self.class, @selector(lz_method1));
IMP result = method_setImplementation(method, imp);
NSLog(@"lz_method_setImplementation:%p",result);
result();
[self lz_method];
}
#pragma mark - 获取方法的结构体
- (void)lz_method_getDescription {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
struct objc_method_description *result = method_getDescription(method);
NSLog(@"lz_method_getDescription:%p",result);
}
#pragma mark - 获取单个参数的类型
- (void)lz_method_getArgumentType {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
char result[10];
method_getArgumentType(method, 0, result, sizeof(result));
NSLog(@"lz_method_getArgumentType:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取方法参数的数量
- (void)lz_method_getNumberOfArguments {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
unsigned int result = method_getNumberOfArguments(method);
NSLog(@"lz_method_getNumberOfArguments:%d",result);
}
#pragma mark - 获取方法的返回值类型
- (void)lz_method_getReturnType {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
char result[10];
method_getReturnType(method,result,sizeof(result));
NSLog(@"lz_method_getReturnType:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取方法的单个参数类型
- (void)lz_method_copyArgumentType {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
char *result = method_copyArgumentType(method, 0);
NSLog(@"lz_method_copyReturnType:%@",[NSString stringWithUTF8String:result]);
free(result);
}
#pragma mark - 获取方法的返回值类型
- (void)lz_method_copyReturnType {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
char *result = method_copyReturnType(method);
NSLog(@"lz_method_copyReturnType:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取描述方法的参数和返回值类型的字符串
- (void)lz_method_getTypeEncoding {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
const char *result = method_getTypeEncoding(method);
NSLog(@"lz_method_getTypeEncoding:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取方法的实现
- (void)lz_method_getImplementation {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
IMP result = method_getImplementation(method);
NSLog(@"lz_method_getImplementation:%p",result);
}
#pragma mark - 获取方法名称
- (void)lz_method_getName {
Method method = class_getInstanceMethod(self.class, @selector(lz_method));
SEL result = method_getName(method);
NSLog(@"lz_method_getName:%p",result);
}
#pragma mark - 删除关联
- (void)lz_objc_removeAssociatedObjects {
objc_removeAssociatedObjects(self);
}
#pragma mark - 获取关联的值
- (void)lz_objc_getAssociatedObject {
id result = objc_getAssociatedObject(self, [@"LZKey" UTF8String]);
NSLog(@"lz_objc_getAssociatedObject:%@",result);
}
#pragma mark - 设置关联
- (void)lz_objc_setAssociatedObject {
objc_setAssociatedObject(self, [@"LZKey" UTF8String], @"123456", OBJC_ASSOCIATION_COPY_NONATOMIC);
}
#pragma mark - 获取实例变量的偏移量
- (void)lz_ivar_getOffset {
Ivar ivar = class_getInstanceVariable(self.class, [@"_lz" UTF8String]);
ptrdiff_t result = ivar_getOffset(ivar);
NSLog(@"lz_ivar_getOffset:%td",result);
}
#pragma mark - 获取实例变量的类型
- (void)lz_ivar_getTypeEncoding {
Ivar ivar = class_getInstanceVariable(self.class, [@"_lz" UTF8String]);
const char *result = ivar_getTypeEncoding(ivar);
NSLog(@"lz_ivar_getTypeEncoding:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取实例变量名称
- (void)lz_ivar_getName {
Ivar ivar = class_getInstanceVariable(self.class, [@"_lz" UTF8String]);
const char *result = ivar_getName(ivar);
NSLog(@"lz_ivar_getName:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取指定类的元类定义
- (void)lz_objc_getMetaClass {
id result = objc_getMetaClass([@"ViewController" UTF8String]);
NSLog(@"objc_getMetaClass:%@",result);
}
#pragma mark - 获取指定类的类定义
- (void)lz_objc_getRequiredClass {
Class result = objc_getRequiredClass([@"ViewController" UTF8String]);
NSLog(@"lz_objc_getRequiredClass:%@",result);
}
#pragma mark - 获取指定类的类定义
- (void)lz_objc_getClass {
Class result = objc_getClass([@"ViewController" UTF8String]);
NSLog(@"lz_objc_getClass:%@",result);
}
#pragma mark - 获取指定类的类定义
- (void)lz_objc_lookUpClass {
Class result = objc_lookUpClass([@"ViewController" UTF8String]);
NSLog(@"lz_objc_lookUpClass:%@",result);
}
#pragma mark - 获取已注册的类
- (void)lz_objc_copyClassList {
unsigned int outCount;
Class *result = objc_copyClassList(&outCount);
for (int i = 0; i < outCount; i++) {
NSLog(@"lz_objc_copyClassList:%s", class_getName(result[i]));
}
free(result);
}
#pragma mark - 获取已注册的类的数量
- (void)lz_objc_getClassList {
int result = objc_getClassList(NULL, 0);
NSLog(@"lz_objc_getClassList:%d",result);
}
#pragma mark - 设置对象的类
- (void)lz_object_setClass {
Class result = object_setClass(self, [UIViewController class]);
NSLog(@"lz_object_setClass:%@",result);
}
#pragma mark - 获取对象的类对象
- (void)lz_object_getClass {
Class result = object_getClass(self);
NSLog(@"lz_object_getClass:%@",result);
}
#pragma mark - 获取对象的类名
- (void)lz_object_getClassName {
const char*result = object_getClassName(self);
NSLog(@"lz_object_getClassName:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 设置对象中实例变量的值
- (void)lz_object_setIvar {
Ivar ivar = class_getInstanceVariable(self.class, [@"_lz" UTF8String]);
object_setIvar(self, ivar, @"123456");
}
#pragma mark - 获取对象中实例变量的值
- (void)lz_object_getIvar {
Ivar ivar = class_getInstanceVariable(self.class, [@"_lz" UTF8String]);
NSString *result = object_getIvar(self, ivar);
NSLog(@"lz_object_getIvar:%@",result);
}
#pragma mark - 实例化类
- (void)lz_class_createInstance {
NSObject *result = class_createInstance([NSObject class], 0);
NSLog(@"lz_class_createInstance:%@",result);
}
#pragma mark - 注册使用类
- (void)lz_objc_registerClassPair {
Class result = objc_allocateClassPair([NSObject class], [@"NewClass" UTF8String], 0);
objc_registerClassPair(result);
}
#pragma mark - 销毁一个类
- (void)lz_objc_disposeClassPair {
Class result = objc_allocateClassPair([NSObject class], [@"NewClass" UTF8String], 0);
objc_disposeClassPair(result);
}
#pragma mark - 添加一个新类
- (void)lz_objc_allocateClassPair {
Class result = objc_allocateClassPair([NSObject class], [@"NewClass" UTF8String], 0);
NSLog(@"lz_objc_allocateClassPair:%p",result);
}
#pragma mark - 设置类的版本号
- (void)lz_class_setVersion {
class_setVersion(self.class, 100);
}
#pragma mark - 获取类的版本号
- (void)lz_class_getVersion {
int result = class_getVersion(self.class);
NSLog(@"lz_class_getVersion:%d",result);
}
#pragma mark - 获取协议列表
- (void)lz_class_copyProtocolList {
unsigned int copyProtocolListCount = 0;
Protocol * __unsafe_unretained *protocals = class_copyProtocolList([self class], ©ProtocolListCount);
for (NSInteger i = 0; i < copyProtocolListCount; i++) {
Protocol * protocal = protocals[i];
const char *name = protocol_getName(protocal);
NSLog(@"lz_class_copyProtocolList:%s",name);
}
free(protocals);
}
#pragma mark - 判断是否遵循某个协议
- (void)lz_class_conformsToProtocol {
BOOL result = class_conformsToProtocol(self.class, NSProtocolFromString(@"UITableViewDelegate"));
NSLog(@"lz_class_conformsToProtocol:%d",result);
}
#pragma mark - 替换类的属性
- (void)lz_class_replaceProperty {
objc_property_attribute_t type = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership = { "C", "" };
objc_property_attribute_t backingivar = { "V", "_attribute0" };
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
class_replaceProperty(self.class, [@"lz" UTF8String], attrs, 3);
}
#pragma mark - 为类添加属性
NSString *attribute0Getter(id classInstance, SEL _cmd) {
Ivar ivar = class_getInstanceVariable([classInstance class], "_attribute0");
return object_getIvar(classInstance, ivar);
}
void attribute0Setter(id classInstance, SEL _cmd, NSString *newName) {
Ivar ivar = class_getInstanceVariable([classInstance class], "_attribute0");
id oldName = object_getIvar(classInstance, ivar);
if (oldName != newName) object_setIvar(classInstance, ivar, [newName copy]);
}
- (void)lz_class_addProperty {
objc_property_attribute_t type = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership = { "C", "" };
objc_property_attribute_t backingivar = { "V", "_attribute0" };
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
BOOL suc0 = class_addProperty(self.class, "_attribute0", attrs, 3);
SEL getter = NSSelectorFromString(@"attribute0");
SEL setter = NSSelectorFromString(@"setAttribute0:");
BOOL suc1 = class_addMethod(self.class, getter, (IMP)attribute0Getter, "@@:");
BOOL suc2 = class_addMethod(self.class, setter, (IMP)attribute0Setter, "v@:@");
NSLog(@"lz_class_addProperty:class_addProperty = %d,class_addMethod_Getter = %d,class_addMethod_Setter = %d",suc0,suc1,suc2);
}
#pragma mark - 添加协议
-(void)lz_class_addProtocol {
BOOL result = class_addProtocol([self class], NSProtocolFromString(@"UITableViewDelegate"));
NSLog(@"lz_class_addProtocol:%d",result);
}
#pragma mark - 判断是否响应方法
-(void)lz_class_respondsToSelector {
BOOL result = class_respondsToSelector(self.class, @selector(viewDidLoad));
NSLog(@"lz_class_respondsToSelector:%d",result);
}
#pragma mark - 获取方法实现
- (void)lz_class_getMethodImplementation {
IMP result = class_getMethodImplementation([ViewController class], @selector(lz_method));
result();
}
- (void)lz_method {
NSLog(@"lz_method lz_method lz_method");
}
#pragma mark - 交换方法
- (void)lz_class_replaceMethod {
[self lz_method1];
BOOL result = class_replaceMethod([self class], @selector(lz_method1), [self methodForSelector:@selector(lz_method2)], NULL);
NSLog(@"lz_class_replaceMethod:%d",result);
[self lz_method1];
}
- (void)lz_method1 {
NSLog(@"lz_method1");
}
- (void)lz_method2 {
NSLog(@"lz_method2");
}
#pragma mark - 获取整个类的实例方法
- (void)lz_class_copyMethodList {
unsigned int copycopyMethodListCount = 0;
Method *methods = class_copyMethodList([self class], ©copyMethodListCount);
for (NSInteger i = 0; i < copycopyMethodListCount; i++) {
Method method = methods[i];
SEL name = method_getName(method);
NSLog(@"lz_class_copyMethodList:%@",NSStringFromSelector(name));
}
free(methods);
}
#pragma mark - 获取类方法
- (void)lz_class_getClassMethod {
Method result = class_getClassMethod(self.class, @selector(lzClassMethod));
NSLog(@"lz_class_getClassMethod:%p",result);
}
+ (void)lzClassMethod {
NSLog(@"我是类方法");
}
#pragma mark - 获取类的实例方法
- (void)lz_class_getInstanceMethod {
Method result = class_getInstanceMethod(self.class, @selector(lz_method));
NSLog(@"lz_class_getInstanceMethod:%p",result);
}
#pragma mark - 给类添加方法
- (void)lz_addMethod {
BOOL result = class_addMethod(self.class, NSSelectorFromString(@"lzMethod"), [self methodForSelector:@selector(lz_method)], "v@:");
NSLog(@"lz_addMethod:%d",result);
}
#pragma mark - 获取整个属性列表
- (void)lz_class_copyPropertyList {
unsigned int copyPropertyListCount = 0;
objc_property_t *propertys = class_copyPropertyList([self class], ©PropertyListCount);
for (NSInteger i = 0; i < copyPropertyListCount; i++) {
objc_property_t property = propertys[i];
const char *name = property_getName(property);
NSLog(@"lz_class_copyPropertyList:%s",name);
}
free(propertys);
}
#pragma mark - 获取指定的属性
- (void)lz_class_getProperty {
objc_property_t result = class_getProperty(self.class, [@"lz" UTF8String]);
NSLog(@"lz_class_getProperty:%p",result);
}
#pragma mark - 获取类的weak修饰的实例变量
- (void)lz_class_getWeakIvarLayout {
const uint8_t *result = class_getWeakIvarLayout(self.class);
NSLog(@"lz_class_getIvarLayout:%p",result);
}
#pragma mark - 设置类的实例变量的修饰符为weak
- (void)lz_class_setWeakIvarLayout{
uint8_t u = 1;
const uint8_t *u8 = &u;
class_setWeakIvarLayout(self.class, u8);
}
#pragma mark - 设置类的实例变量的修饰符
- (void)lz_class_setIvarLayout {
uint8_t u = 1;
const uint8_t *u8 = &u;
class_setIvarLayout(self.class, u8);
}
#pragma mark - 获取类的实例变量的修饰符
- (void)lz_class_getIvarLayout {
const uint8_t *result = class_getIvarLayout(self.class);
NSLog(@"lz_class_getIvarLayout:%p",result);
}
#pragma mark - 获取整个成员变量列表
- (void)lz_class_copyIvarList {
unsigned int copyIvarListCount = 0;
Ivar *ivars = class_copyIvarList([self class], ©IvarListCount);
for (NSInteger i = 0; i< copyIvarListCount; i ++) {
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
NSLog(@"lz_class_copyIvarList:%s",name);
}
free(ivars);
}
#pragma mark - 为动态创建的类添加变量
- (void)lz_class_addIvar {
Class CreatClass0 = objc_allocateClassPair([NSObject class], "CreatClass0", 0);
class_addIvar(CreatClass0, "_attribute0", sizeof(NSString *), log(sizeof(NSString *)), "i");
Ivar ivar = class_getInstanceVariable(CreatClass0, "_attribute0");
NSLog(@"lz_class_addIvar:%@",[NSString stringWithUTF8String:ivar_getName(ivar)]);
objc_registerClassPair(CreatClass0);
}
#pragma mark - 获取指定的类变量的信息的数据结构的指针
- (void)lz_class_getClassVariable {
Ivar ivar = class_getClassVariable(self.class, [@"isa" UTF8String]);
NSLog(@"lz_class_getClassVariable:%p",ivar);
}
#pragma mark - 判断类是否为元类
- (void)lz_class_isMetaClass {
BOOL result = class_isMetaClass([ViewController class]);
NSLog(@"lz_class_isMetaClass:%d",result);
}
#pragma mark - 获取父类
- (void)lz_class_getSuperclass {
Class result = class_getSuperclass([ViewController class]);
NSLog(@"lz_class_getName:%@",result);
}
#pragma mark - 获取类名
- (void)lz_class_getName {
const char *result = class_getName([ViewController class]);
NSLog(@"lz_class_getName:%@",[NSString stringWithUTF8String:result]);
}
#pragma mark - 获取实例大小
- (void)lz_class_getInstanceSize {
size_t result = class_getInstanceSize([ViewController class]);
NSLog(@"lz_class_getInstanceSize:%zu",result);
}
#pragma mark - 获取类中指定的实例变量的信息的数据结构的指针
- (void)lz_class_getInstanceVariable {
const char *result1 = [@"_lz" UTF8String];
Ivar result2 = class_getInstanceVariable([self class], result1);
NSLog(@"lz_class_getInstanceVariable:%p",result2);
}
@end