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 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...
随机推荐
- crm2011js操作选项卡和节点
CRM窗口选项卡的操作 crm2011节点的操作
- autolisp 列表 resbuf
有以下 list : (1 2 3 (4 5 6) "Properties" (("id" . 3) ("name" . "hel ...
- swift http请求返回json数据和分析
1 AppDelegate.swift // // AppDelegate.swift // QQDemo // // Created by 赵超 on 14-6-21. // Copyright ( ...
- InfluxDB 开源分布式时序、事件和指标数据库
InfluxDB 是一个开源分布式时序.事件和指标数据库.使用 Go 语言编写,无需外部依赖.其设计目标是实现分布式和水平伸缩扩展. 特点 schemaless(无结构),可以是任意数量的列 Scal ...
- UIButton 设置字体大小
btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...
- 读书笔记-实用单元测试(英文版) Pragmatic Unit Testing in C# with NUnit
读书笔记-实用单元测试(英文版) Pragmatic Unit Testing in C# with NUnit Author: Andrew Hunt ,David Thomas with Matt ...
- autorelease方法
基本用法: 1,autorelease 方法会返回对象本身 2,调用完autorelease方法后,对象的计数器不变 2,autorelease 会将对象放到一个自动释放池中 3,当自动释放池被销 ...
- 关于sql 外键的讨论。
外键是否采用看业务应用场景,以及开发成本的,大致列下什么时候适合,什么时候不适合使用: 1. 互联网行业应用不推荐使用外键: 用户量大,并发度高,为此数据库服务器很容易成为性能瓶颈,尤其受IO能力限制 ...
- media queries(练习)
根据不同的窗口尺寸来选择使用不同的样式的示例 MAIN SUB 01 SUB 02
- Http Analyzer 数据抓包
一.工具简介 这是一款实时分析 HTTP/HTTPS 数据流的工具.它可以实时捕捉HTTP/HTTPS 协议数据,可以显示许多信息(包括:文件头.内容.Cookie.查询字符窜.提交的数据.重定向的U ...