1-runtime的Method,IMP,Property,ivar
基础定义
objc-750 的tar包
objc-private.h 定义
typedef struct objc_class *Class;
typedef struct objc_object *id;#if __OBJC2__
typedef struct method_t *Method;
typedef struct ivar_t *Ivar;
typedef struct category_t *Category;
typedef struct property_t *objc_property_t;
#else
typedef struct old_method *Method;
typedef struct old_ivar *Ivar;
typedef struct old_category *Category;
typedef struct old_property *objc_property_t;
#endif
一 Method 方法(包含SEL 和 IMP)
在 objc-runtime-new.h 文件中 找到 method 结构体的定义
struct method_t {SEL name;const char *types;MethodListIMP imp;struct SortBySELAddress :public std::binary_function<const method_t&,const method_t&, bool>{bool operator() (const method_t& lhs,const method_t& rhs){ return lhs.name < rhs.name; }};
};
1 IMP 本质是函数指针 (Implementation)
是一个函数的指针,保存了方法的地址,每一个方法都对应有一个IMP
2 SEL 类方法名称的描述,只记录方法的编号不记录具体的方法,具体的方法是 IMP。
获取SEL 的两种方式
SEL sel1 = NSSelectorFromString(@"dealloc");SEL sel2 = @selector(viewDidLoad);
获取IMP的两种方式
// 实例方法 - (IMP)methodForSelector:(SEL)aSelector;NSLog(@"%p",[self methodForSelector:sel1]);// 类方法 + (IMP)instanceMethodForSelector:(SEL)aSelector;NSLog(@"%p",[[self class] instanceMethodForSelector:sel2]);
获取方法获取IMP
// 获取一个类的实例的方法Method method = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));// 通过该实例方法IMP 函数指针IMP method_imp = method_getImplementation(method);NSLog(@"%p",method_imp);
二 属性 Property
@property 修饰过的属性,修饰后变为objc_property_t
struct property_t {const char *name;const char *attributes;
};
获取属性列表 和 协议列表
// 获取注册类的所有属性列表objc_property_t * ptys = class_copyPropertyList([obj class], &count);// 获取注册类的所有协议列表objc_property_t * pros = class_copyPropertyList([obj class], &count);
获取一个类的所有属性
// 获取一个类的所有的属性
- (NSMutableArray *)getAllPropertyNames:(id)obj
{unsigned int count;// 获取注册类的所有属性列表objc_property_t * ptys = class_copyPropertyList([obj class], &count);// 获取注册类的所有属性列表NSMutableArray *resArr = [NSMutableArray array];for (int i = 0 ; i < count ; i ++) {objc_property_t pty = ptys[i];// 获得属性名NSString *ptyName = [NSString stringWithFormat:@"%s",property_getName(pty)];NSLog(@"%@",ptyName);[resArr addObject:ptyName];} free(ptys);return resArr;
}
三 成员变量 objc_ivar 类型
ivar 是 objc_ivar 的指针 包含变量名,变量类型,基地址偏移量,在对成员变量寻址时使用。
struct objc_ivar {char *ivar_name;char *ivar_type;int ivar_offset;#ifdef __LP64__int space;#endif}
获取一个类所有的成员变量别表 class_copyIvarList
后去一个类所有的成员变量的名字 ivar_getName
成员变量和属性的关系,从下面可以看出来,一个属性是对应一个成员变量的,属性是根据自己的属性特性定义来对这个成员变量进行一系列的封装,getter setter方法,线程安全,内存管理操作。
但是有成员变量不一定有属性,当且仅当有property 修饰的时候 才会有属性。
@interface Student : NSObject{NSString *lastName;
}
/** */
@property (nonatomic,strong)NSString *username;/** */
@property (nonatomic,strong)NSString *className;@end// 访问一个类的成员变量unsigned int stu_count;// 打印结果// lastname _username _classNameIvar *varList = class_copyIvarList([Student class], &stu_count);for (int i = 0 ; i < stu_count; i ++) {NSLog(@"%s",ivar_getName(varList[i]));}// 打印结果 username className[self getAllPropertyNames:[Student new]];
1-runtime的Method,IMP,Property,ivar相关推荐
- When Runtime.exec() won't Navigate yourself around pitfalls related to the Runtime.exec() method
转载地址:http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html As part of th ...
- “SqlNullValueException: Data is Null. This method or property cannot be called on Null values.”的解决方案
本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:"SqlNullValueException: Data is Null. This method or property ...
- 崩溃边缘 on No getter method for property
崩溃边缘 on No getter method for property 过了大约6个小时.终于把这个异常搞定了.脑袋马上就爆炸!异常如下: No getter method for propert ...
- SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
一.问题描述: Unhandled Exception: System.Data.SqlTypes.SqlNullValueException: Data is Null. This method o ...
- HHH000122: IllegalArgumentException in class....getter method of property: id
HHH000122: IllegalArgumentException in class-.getter method of property: id 我的项目是微服务架构,用的是spring boo ...
- runtime(四) method swizzling 与AOP编程
什么是 AOP : (site: baike.baidu.com),引用百度百科中的解释就是: 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过 ...
- iOS 开发:『Runtime』详解(二)Method Swizzling
本文用来介绍 iOS 开发中『Runtime』中的黑魔法Method Swizzling. 通过本文,您将了解到: Method Swizzling(动态方法交换)简介 Method Swizzlin ...
- iOS_selector、SEL、IMP、Method都是什么,以及之间的关系
文章目录 一.SEL(selector) 二.IMP(implementation) 三.Method 总结 在 Objective-C中使用发送消息的形式来调用方法,其中涉及到 Runtime库 ...
- iOS总结-Runtime篇之黑魔法Method Swizzling的滥用会有危险吗
参考https://www.jianshu.com/p/19c5736c5d9a, http://blog.sina.com.cn/s/blog_a343f32b0101en4o.html runti ...
最新文章
- python3.4用循环往mysql5.7中写数据并输出
- 09_Mybatis开发Dao方法——mapper代理开发规范
- ReactiveCocoa的使用方法
- 20135213——信息安全系统设计基础第十周学习总结
- RDS数据库全量恢复方案
- 2022年面试,整理全网初、中、高级常见 Java 面试题
- python查看哪些内存被释放_python内存不释放原理 | shell's home
- 信号与系统3-傅里叶变换与频域分析
- Halcon——颜色识别提取
- 【大咖说:绿色低碳的生活该是什么样子?】
- java基础(部分)
- Hexo Next主题中集成gitalk评论系统
- 【第39天】实现一个冒泡排序
- 【时间序列】001-推断统计
- 北方民族大学计算机科学与导论试题,2016秋计算机导论(北方民族大学 张春梅)...
- 安全运维 | Linux系统基线检查
- python颜色识别_Python人体肤色检测
- ad9850c语言编程,AD9850与单片机波形生成程序及设计
- Php ajax 动态 下载,jQuery动态实现ajax文件下载
- MATLAB批量求图片均值、相对标准差、平均梯度、信息熵的代码
热门文章
- hdu 2199 Can you solve this equation? 二分
- 团队作业—第二阶段06
- 7个华丽的基于Canvas的HTML5动画
- UIActionSheet在iOS8中被弃用造成的错误
- HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求。...
- python数据结构与算法:排序算法(面试经验总结)
- php报错Permission denied
- 函数指针--Nginx和Redis中两种回调函数写法
- 【FFmpeg】警告:[mpegts] H.264 bitstream error, startcode missing, size 0
- 荣耀预装linux安装包,关于华为机子没有预装应用商店讨论