iOS进阶:Objective-C runtime(一)
第一次看到runtime时,觉得太高大上,动态获取方法、属性等简直厉害的不要不要的。在经过查找资料+实践后,发现runtime并没有想象中那么复杂,接下来对runtime进行基本的介绍。
要使用运行时方法需要引入runtime.h文件
一、基础知识
Method :成员方法
Ivar : 成员变量
二、常用方法
class_copyPropertyList : 获取属性列表
class_copyMethodList : 获取成员方法列表
class_copyIvarList:获取成员变量列表
ivar_getName:获取变量名
property_getName:获取属性名
使用示例:
1.获取成员变量列表
//1.获取变量list
unsigned int ivarCount = ; //成员变量数
Ivar *ivarList = class_copyIvarList([self class], &ivarCount);//ivar数组 for (int i = ; i < ivarCount; i++) {//遍历
Ivar ivar = ivarList[i]; //获取ivar
const char *name = ivar_getName(ivar);//获取变量名
NSString *key = [NSString stringWithUTF8String:name];
NSLog(@"%@", key); } free(ivarList);
2.获取属性列表
unsigned int count = ;
objc_property_t *propertList = class_copyPropertyList([self class], &count);
for (int i = ; i < count; i++) {
objc_property_t property = propertList[i];
const char *name = property_getName(property);
const char *attrs = property_getAttributes(property);
// property_copyAttributeValue(,) 第一个参数为objc_property_t,第二个参数"V"获取变量名,"T"获取类型
const char *value = property_copyAttributeValue(property, "V");
NSLog(@"name = %s, attrs = %s, value = %s", name, attrs, value);
}
free(propertList);
3.获取方法列表
unsigned int count = ;
Method *methodList = class_copyMethodList([self class], &count);
for (int i = ; i < count; i++) {
Method method = methodList[i];
SEL selector = method_getName(method);//方法入口
const char *sel_name = sel_getName(selector);
NSLog(@"方法名 %s", sel_name);
}
free(methodList);
三、使用方向:归档、字典<---->模型、框架封装等
实现归档
#define WKCodingImplementing \
- (void)encodeWithCoder:(NSCoder *)aCoder \
{ \
unsigned int ivarCount = ; \
Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \
for (int i = ; i < ivarCount; i++) { \
Ivar ivar = ivarList[i]; \
const char *name = ivar_getName(ivar); \
const char *type = ivar_getTypeEncoding(ivar); \
NSLog(@"%s-----%s", name, type); \
NSString *key = [NSString stringWithUTF8String:name]; \
id value = [self valueForKey:key]; \
[aCoder encodeObject:value forKey:key]; \
} \
free(ivarList); \
} \
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder \
{ \
if (self = [super init]) { \
unsigned int ivarCount = ; \
Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \
for (int i = ; i < ivarCount; i++) { \
Ivar ivar = ivarList[i]; \
const char *name = ivar_getName(ivar); \
NSString *key = [NSString stringWithUTF8String:name]; \
NSLog(@"%@ %@", key, value); \
id value = [aDecoder decodeObjectForKey:key]; \
[self setValue:value forKey:key]; \
} \
} \
return self; \
}
iOS进阶:Objective-C runtime(一)的更多相关文章
- iOS开发之使用Runtime给Model类赋值
本篇博客算是给网络缓存打个基础吧,本篇博客先给出简单也是最容易使用的把字典转成实体类的方法,然后在给出如何使用Runtime来给Model实体类赋值.本篇博客会介绍一部分,主要是字典的key与Mode ...
- iOS进阶_地图上定位的标志——大头针
一.添加大头针 地图使用的框架是MapKit 大头针走的是MKAnnotation协议 /* 注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大 ...
- iOS进阶指南试读之UI篇
iOS进阶指南试读之UI篇 UI篇 UI是一个iOS开发工程师的基本功.怎么说?UI本质上就是你调用苹果提供给你的API来完成设计师的设计.所以,想提升UI的功力也很简单,没事就看看UIKit里的各个 ...
- iOS开发小技巧 - runtime适配字体
iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...
- (转发)IOS高级开发~Runtime(四)
用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...
- (转发)IOS高级开发~Runtime(三)
11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...
- (转发)IOS高级开发~Runtime(二)
一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...
- (转发)IOS高级开发~Runtime(一)
IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...
- iOS开发——高级特性&Runtime运行时特性详解
Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...
随机推荐
- JSP指令 include 和forward
包括指令:include 静态包括 <%@include file="要包括的文件路径" %> 先将所包括 ...
- 3-07. 求前缀表达式的值(25) (ZJU_PAT数学)
题目链接:http://pat.zju.edu.cn/contests/ds/3-07 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.前缀表达式指二元运算符位于两个运算数之前,比如2+3*(7 ...
- Tsinghua dsa mooc pa1
第一题Range 关键:二分查找,查找不大于一个数的最大下标. #include <cstdlib> #include <cstdio> 4 int compare (cons ...
- 【二进制拆分多重背包】【HDU1059】【Dividing】
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- JavaScript 【跨浏览器处理XML,做个兼容】
//兼容方法 function getXMLDOM(xmlStr) { var xmlDom = null; if (typeof windo ...
- C#核编之System.Console类
顾名思义,Console类封装了基于控制台的输入输出和错误流的操作,下面列举一些System.Console类常用的成员的,这些成员能为简单的命令行程序添加一些"情趣",例如改变背 ...
- hibernate-annotation CascadeType.PERSIST不起作用的解决方法
有如下两个实体类 , Student和Grade 为多对一关系. Student.java @Entity public class Student { private Integer id; pri ...
- HDU 1027 - Ignatius and the Princess II
第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...
- Handle 消息机制
android中Handle类的主要作用: 1.在新启动的线程中发送给消息 2.在主线程获取.处理消息 为什么要用Handle这样的一个机制: 因为在Android系统中UI操作并不是线程安全的,如果 ...
- cURL模拟POST方式提交数据
curl_post.php文件: 1 $url = 'http://localhost/test/curl_post_deal.php'; 2 3 $post_data = array( 4 'use ...