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 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...
随机推荐
- VMware Workstation虚拟机使用ISO映像文件
VMware Workstation虚拟机使用ISO映像文件 VMware Workstation虚拟机使用ISO映像文件
- 一道google面试题(dp)
输入n,把1-n分成两个和相等的子集,有多少种分法 想了个dp,直接背包也行 #include <iostream> #include <cstdio> using names ...
- check cable connection PXE-M0F: Exiting intel PXE ROM no bootable device-- insert boot disk and pre
今天修电脑遇到一个问题,新买的电脑的原装的是linux,然后我按常规方式进入PE后重装系统,然后开机一直显示下面的代码,进不去: check cable connection PXE-M0F: Exi ...
- .net TxetBox控件设置ReadOnly=True后台取值问题
1.为TxetBox添加onfocus=this.blur()进行模拟 2.通过 Request.From["TextBox"].Trim()取值; 3.后台CS文件设置TextB ...
- 彻底解决TAP(点透)提升移动端点击响应速度
使用fastclick 尼玛使用太简单了,直接一句: FastClick.attach(document.body); 于是所有的click响应速度直接提升,刚刚的!什么input获取焦点的问题也解决 ...
- 简单的javascript实例一(时钟特效)
方便以后copy 时钟特效 <html> <head> <meta http-equiv="Content-Type" content="t ...
- iOS 9适配技巧(更新版)
转自: http://www.cocoachina.com/ios/20150929/13598.html 中文快速导航: 1.iOS9网络适配_ATS:改用更安全的HTTPS(见Demo1) i ...
- 在ios8上 [TableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];不起作用 经过测试加入下面方法 在ios7 8上都可以正常工作
-(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] ...
- TSF自定义候选词列表界面
概述 TSF(Text Service Framework),已经取代IMM(Input Method Manager),成为win8+系统的输入法框架.现在有个需求,触摸屏上要使用软键盘(虚拟键盘, ...
- [string]Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...