1》runtime实现的机制是什么,怎么用,一般用于干嘛?
runtime是一套比较底层的纯C语言API, 属于1个C语言库, 包含了很多底层的C语言API。
在我们平时编写的OC代码中, 程序运行过程时, 其实最终都是转成了runtime的C语言代码,
runtime算是OC的幕后工作者
比如说,下面一个创建对象的方法中,
举例:
举例:
[[MJPerson alloc] init]
objc_msgSend(objc_msgSend(“MJPerson” , “alloc”), “init”)
第二个问题
runtime 用来干什么呢??用在那些地方呢?怎么用呢?
runtime是属于OC的底层, 可以进行一些非常底层的操作(用OC是无法现实的, 不好实现)
runtime是属于OC的底层, 可以进行一些非常底层的操作(用OC是无法现实的, 不好实现)
- 在程序运行过程中, 动态创建一个类(比如KVO的底层实现)
- 在程序运行过程中, 动态地为某个类添加属性\方法, 修改属性值\方法
- 遍历一个类的所有成员变量(属性)\所有方法
例如:我们需要对一个类的属性进行归档解档的时候属性特别的多,这时候,我们就会写很多对应的代码, - 但是如果使用了runtime就可以动态设置! 例如,PYPerson.h的文件如下所示
@interface PYPerson : NSObject
@property (nonatomic, assign) int age;
@property (nonatomic, assign) int height;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age2;
@property (nonatomic, assign) int height2;
@property (nonatomic, assign) int age3;
@property (nonatomic, assign) int height3;
@property (nonatomic, assign) int age4;
@property (nonatomic, assign) int height4;
@end
而PYPerson.m实现文件的内容如下
#import "PYPerson.h"
@implementation PYPerson
-(void)encodeWithCoder:(NSCoder )encoder
{
{
unsigned int count = 0;
Ivar ivars = class_copyIvarList([PYPerson class], &count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量 Ivar ivar = ivars[i];
// 查看成员变量 const char *name = ivar_getName(ivar);
// 归档 NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[encoder encodeObject:value forKey:key];
}
free(ivars);
}
}
-(id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
{
if (self = [super init]) {
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([PYPerson class], &count);
for (int i = 0; i<count; i++)
{
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 查看成员变量
const char *name = ivar_getName(ivar);
// 归档 NSString *key = [NSString stringWithUTF8String:name];
id value = [decoder decodeObjectForKey:key];
// 设置到成员变量身上
[self setValue:value forKey:key]; } free(ivars);
}
return self;
}
return self;
}
@end
这样我们可以看到归档和解档的案例其实是runtime写下的
学习,runtime机制首先要了解下面几个问题
1相关的头文件和函数
1> 头文件
1相关的头文件和函数
1> 头文件
- <objc/runtime.h>
- <objc/message.h>
- 利用头文件,我们可以查看到runtime中的各个方法!
2> 相关应用
- NSCoding(归档和解档, 利用runtime遍历模型对象的所有属性)
- 字典 –> 模型 (利用runtime遍历模型对象的所有属性, 根据属性名从字典中取出对应的值, 设置到模型的属性上)
- KVO(利用runtime动态产生一个类)
- 用于封装框架(想怎么改就怎么改)
这就是我们runtime机制的只要运用方向
3> 相关函数
- objc_msgSend : 给对象发送消息
- class_copyMethodList : 遍历某个类所有的方法
- class_copyIvarList : 遍历某个类所有的成员变量
- class_…..
这是我们学习runtime必须知道的函数!
4.必备常识
1> Ivar : 成员变量
2> Method : 成员方法
从上面例子中我们看到我们定义的成员变量,如果要是动态创建方法,可以使用Method,
1> Ivar : 成员变量
2> Method : 成员方法
从上面例子中我们看到我们定义的成员变量,如果要是动态创建方法,可以使用Method,
我理解的iOS中的RunTime机制(2)
一、首先我们要先知道object-c是一门编译型、动态语言(这里强调下oc是静态类型语言),这在开发语言中是并多见的,一般的动态语言多为解释性语言。oc之所以能够做到即使编译型语言,又是动态语言。就是得益于RunTime机制。
由于本文主要讲解的是RunTime机制,所以语言类型不做过多描述,之后可以另写一篇语言类型的介绍。
二、这里主要讲解RunTime如何使用,其中主要的知识点如下:
1、class_copyPropertyList 获取一份拷贝的成员列表数组
2、property_getName获取成员名称
3、class_getInstanceVariable 获取成员对象的Ivar
4、object_getIvar从Ivar对象中取值
5、object_setIvar赋值函数
下面我们来看一下代码实现:
1、创建一个类
CustomClass.h
#import <Foundation/Foundation.h>
@interface CustomClass : NSObject
@property (nonatomic , strong) NSString *age;
- (void)test;
@end
CustomClass.m
#import "CustomClass.h"
@implementation CustomClass
- (void)test
{
NSLog(@"这里是CustomClass");
}
@end
接下来创建一个类别来实现RunTime机制
NSObject+RunTimeTest.h
#import <Foundation/Foundation.h>
@interface NSObject (RunTimeTest)
//动态创建一个类,之后为这个类的age属性赋值。
- (id) testRunTime:(NSString *)classname age:(NSString *)age;
@end
NSObject+RunTimeTest.m
#import "NSObject+RunTimeTest.h"
#import <objc/runtime.h>
#import "CustomClass.h"
@implementation NSObject (RunTimeTest)
-(CustomClass *)testRunTime:(NSString *)classname age:(NSString *)age
{
// propertyCount 成员属性的数量
unsigned int propertyCount = 0;
/* objc_property_t 根据苹果文档的解释:An opaque type that represents an Objective-C declared property.
个人理解:一个任意类型的对象代表一个oc声明的属性成员 (本人英语水平实在有限)
*/
/* class_copyPropertyList 文档:Return Value
An array of pointers of type objc_property_t describing the properties declared by the class.
Any properties declared by superclasses are not included.
The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
个人理解:主要2个意思:返回一个数组指针,类型为objc_property_t,
用完要记得free,释放掉。
*/
objc_property_t *properties = class_copyPropertyList([self class], &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++)
{ objc_property_t property = properties[i];
//获取成员的名称
NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
NSLog(@"propertyName = %@ -- 成员名称",propertyName);
//获取成员内容的Ivar
Ivar iVar = class_getInstanceVariable([self class], [propertyName UTF8String]);
//其实上面那行获取代码是为了保险起见,基本是获取不到内容的。因为成员的名称默认会在前面加"_" ,
if (iVar == nil) {
iVar = class_getInstanceVariable([self class], [[NSString stringWithFormat:@"_%@",propertyName] UTF8String]);
}
// 取值 id propertyVal = object_getIvar(self, iVar);
NSLog(@"propertyVal = %@ --值",propertyVal);
// 动态创建类
Class varClass = NSClassFromString(classname);
id varObj = [[varClass alloc] init];
//调用新创建类的方法,这里要主要一定引入对应得头文件,不然调用方法会不错。
[varObj test];
Ivar iVarObj = class_getInstanceVariable(varClass, [@"_age" UTF8String]);
object_setIvar(varObj, iVarObj, age);
return varObj;
}
return nil;
}
@end
测试:
ViewController.m
#import "NSObject+RunTimeTest.h"
#import "CustomClass.h"
@interface ViewController ()
@property (nonatomic , strong) NSString *name;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_name = @"xiaonan";
CustomClass *tmp = [self testRunTime:@"CustomClass" age:@"098"];
NSLog(@"tmp.age = %@",tmp.age);
}
结果输出:
2014-11-29 17:16:55.483 TestRunTime2[21001:176367] propertyName = name -- 成员名称
2014-11-29 17:16:55.483 TestRunTime2[21001:176367] propertyVal = xiaonan --值
2014-11-29 17:16:55.484 TestRunTime2[21001:176367] 这里是CustomClass
2014-11-29 17:16:55.484 TestRunTime2[21001:176367] tmp.age = 098