关于Runtime
Runtime根据字面理解就是运行时,当我们的代码运行的时候所体现的东西,举一个比较简单一点的例子,这里有一个Person类,然后创建一个Student类继承Person,这里我们知道可以用Person类的对象来接收Student类创建的对象,从代码看来这个对象时一个Person对象,但是在代码运行的时候,这个对象体现出来的却是一个Student对象。
Runtime API
获取对象的类
Class object_getClass(id obj)
设置对象的类
Class object_setClass(id obj, Class cls)
获取对象的类名
const char *object_getClassName(id obj)
获取实例变量的值
id object_getIvar(id obj, Ivar ivar)
设置实例变量的值 这个方法比下面这个设置实例变量要快
void object_setIvar(id obj, Ivar ivar, id value)
设置实例变量的值
Ivar object_setInstanceVariable(id obj, const char *name, void *value)
获取实例变量变量和值
Ivar object_getInstanceVariable(id obj, const char *name, void **outValue)
根据名称获取类
Class objc_getClass(const char *name)
获取对应类和实例变量名的Ivar指针
Ivar class_getInstanceVariable(Class cls, const char *name)
获取类对应的实例变量的Ivar指针数组
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
根据类名和SEL变量获取实例方法对象
Method class_getInstanceMethod(Class cls, SEL name)
根据类名和SEL变量获取类方法对象
Method class_getClassMethod(Class cls, SEL name)
根据类和方法名获取IMP指针 这个方法比IMP method_getImplementation(Method m) 可能要快一点
IMP类型是(void(*)(id instance, SEL _cmd, id parameter1,...))
IMP class_getMethodImplementation(Class cls, SEL name)
类实例能否响应这个SEL
BOOL class_respondsToSelector(Class cls, SEL sel)
根据name获取属性
objc_property_t class_getProperty(Class cls, const char *name)
获取属性列表
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
给指定类添加新方法
class_addMethod(Class cls, SEL name, IMP imp,
const char *types)
取代一个方法的实现
IMP class_replaceMethod(Class cls, SEL name, IMP imp,
const char *types)
添加一个实例变量
这里举一个例子
class_addIvar(newClass, "name", sizeof(NSString *), log2(sizeof(NSString *)), @encode(NSString *));
BOOL class_addIvar(Class cls, const char *name, size_t size,
uint8_t alignment, const char *types)
添加一个属性
attributes这个参数的顺序要保持T(Type)在第一位,V(Ivar)在最后一位
BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
替换属性
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
创建一个新类
Class objc_allocateClassPair(Class superclass, const char *name,
size_t extraBytes)
注册这个新类 给创建的新类添加属性 方法 协议 成员变量等都需要在注册之前完成
void objc_registerClassPair(Class cls)
Method转SEL变量
SEL method_getName(Method m)
Method转IMP指针
IMP method_getImplementation(Method m)
设置一个Method的IMP指针 返回之前的IMP指针
IMP method_setImplementation(Method m, IMP imp)
交换两个Method
void method_exchangeImplementations(Method m1, Method m2)
获取变量名
const char *ivar_getName(Ivar v)
获取变量的类型编码
const char *ivar_getTypeEncoding(Ivar v)
获取属性名
const char *property_getName(objc_property_t property)
获取属性的特性
const char *property_getAttributes(objc_property_t property)
获取属性特性数组
objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount)
根据属性的特性name获取value
char *property_copyAttributeValue(objc_property_t property, const char *attributeName)
SEL转c字符串
const char *sel_getName(SEL sel)
根据str返回一个SEL变量
SEL sel_getUid(const char *str)
根据str注册一个SEL变量
SEL sel_registerName(const char *str)
判断两个SEL变量是否相等
BOOL sel_isEqual(SEL lhs, SEL rhs)
block转IMP
block举例 由于是block所以没有SEL参数
id block = ^(id instance, id parameter1, id parameter2) {
};
IMP imp_implementationWithBlock(id block)
IMP转block
id imp_getBlock(IMP anImp)
给一个对象关联一个指定的key和关联方式
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
获取关联值
id objc_getAssociatedObject(id object, const void *key)
移除关联值
void objc_removeAssociatedObjects(id object)
IMP SEL Method是可以相互转化的,下面上一张图,做的不怎么好的,凑合着看吧
IMP&SEL&Method
Type Encoding
Type Encoding关于属性的特性我这里放一个链接
objc_property_attribute_t
实际应用
给Category添加属性
我就随便给一个Category添加一个莫名其妙的属性
static const void *countKey = &countKey;
@implementation NSObject (Count)
- (void)setCount:(NSInteger)count {
objc_setAssociatedObject(self, countKey, @(count), OBJC_ASSOCIATION_ASSIGN);
}
- (NSInteger)count {
return [objc_getAssociatedObject(self, countKey) integerValue];
}
@end
拦截系统的方法(swizzling)
我这里根据IMP SEL Method的关系,我想到三种实现的方法
static IMP _setColor;
static const void *pageKey = &pageKey;
static const void *nameKey = &nameKey;
@implementation UIView (Border)
+ (void)load {
/***********************************交换方法************************/
//方法1
// Method method = class_getInstanceMethod(self, @selector(setBackgroundColor:));
// _setColor = method_setImplementation(method, (IMP)colorOfBackground);
//方法2
_setColor = class_replaceMethod(self, @selector(setBackgroundColor:), (IMP)colorOfBackground, "v@:@");
//方法3
// Method method1 = class_getInstanceMethod(self, @selector(setBackgroundColor:));
// Method method2 = class_getInstanceMethod(self, @selector(colorForBackgroundColor:));
// method_exchangeImplementations(method1, method2);
/***********************************交换方法************************/
}
- (void)colorForBackgroundColor:(UIColor *)color {
// 这里不会循环,因为已经交换方法,调用本身相当于调用系统setBackgroundColor:方法
[self colorForBackgroundColor:color];
NSLog(@"设置颜色成功, 并替换了方法 ---colorForBackgroundColor");
}
void colorOfBackground(UIView *view, SEL sel, UIColor *color) {
((void (*)(UIView *, SEL, UIColor *))_setColor)(view, sel, color);
NSLog(@"设置颜色成功, 并替换了方法 ---colorOfBackground");
}
快速序列化,实现归档和反归档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
unsigned int outCount;
Ivar * ivars = class_copyIvarList([self class], &outCount);
for (int i = 0; i < outCount; i ++) {
Ivar ivar = ivars[i];
NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
[self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int outCount;
Ivar * ivars = class_copyIvarList([self class], &outCount);
for (int i = 0; i < outCount; i ++) {
Ivar ivar = ivars[i];
NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
[aCoder encodeObject:[self valueForKey:key] forKey:key];
}
}
实现自己的KVO
如果要实现自己的KVO就要先了解苹果自带的KVO的实现原理,相信大家都会用KVO,在调用- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;这个方法的时候,会发现当前这个调用者的isa指针发生变化,这里运用了Runtime的东西。实现自己的KVO重点在于重写setter方法和消息的转发。调用msg_Send()是出现错误,在BuildSetting,搜索msg,将YES改为NO
这里为NSObject添加了一个分类,其实系统的KVO也是NSObject的一个分类实现
//
// NSObject+FSKVO.m
// Runtime
//
// Created by vcyber on 17/8/23.
// Copyright © 2017年 vcyber. All rights reserved.
//
#import "NSObject+FSKVO.h"
#import <objc/message.h>
static const void *observerKey = &observerKey;
static const void *keyPathKey = &keyPathKey;
static const void *optionsKey = &optionsKey;
static const void *setterKey = &setterKey;
static const void *oldValueKey = &oldValueKey;
@implementation NSObject (FSKVO)
- (void)fs_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context {
//1.动态生成一个类
NSString *oldClassName = NSStringFromClass([self class]);
NSString *newClassName = [@"FS_" stringByAppendingString:oldClassName];
Class newClass = objc_allocateClassPair([self class], newClassName.UTF8String, 0);
//2.为新生成的类添加setter方法
NSString *setter = [@"set" stringByAppendingString:[[keyPath capitalizedString] stringByAppendingString:@":"]];
class_addMethod(newClass, NSSelectorFromString(setter), (IMP)setKeyPath, "v@:@");
objc_registerClassPair(newClass);
//修改被观察者的isa指针!!让它指向自定义的类!!
object_setClass(self, newClass);
//3.获取旧值
id oldValue = [self valueForKeyPath:keyPath];
//3.保存observer keyPath options setter 旧值 用于消息发送
objc_setAssociatedObject(self, observerKey, observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, keyPathKey, keyPath, OBJC_ASSOCIATION_COPY);
objc_setAssociatedObject(self, optionsKey, @(options), OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(self, setterKey, setter, OBJC_ASSOCIATION_COPY);
objc_setAssociatedObject(self, oldValueKey, oldValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
void setKeyPath(id self, SEL _cmd, id newValue) {
id class = [self class];
//改变当前对象指向父类!!
object_setClass(self, class_getSuperclass(class));
//调用父类的setter方法
NSString *setter = objc_getAssociatedObject(self, setterKey);
objc_msgSend(self, NSSelectorFromString(setter), newValue);
//取出观察者 转发消息
id observer = objc_getAssociatedObject(self, observerKey);
NSString *keyPath = objc_getAssociatedObject(self, keyPathKey);
NSUInteger options = [objc_getAssociatedObject(self, optionsKey) unsignedIntegerValue];
NSMutableDictionary<NSKeyValueChangeKey, id> *change = [NSMutableDictionary dictionary];
if (options & NSKeyValueObservingOptionNew) {
change[NSKeyValueChangeNewKey] = newValue;
}
if (options & NSKeyValueObservingOptionOld) {
change[NSKeyValueChangeOldKey] = objc_getAssociatedObject(self, oldValueKey);
}
objc_msgSend(observer, @selector(observeValueForKeyPath:ofObject:change:context:), keyPath, self, change, NULL);
object_setClass(self, class);
objc_setAssociatedObject(self, oldValueKey, newValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
用法和系统的用法一样
MyClass1 *class1 = [[MyClass1 alloc] init];
_cls = class1;
class1.string = @"test";
[class1 fs_addObserver:self forKeyPath:@"string" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
NSLog(@"%@-%@", keyPath, change);
}
效果
JSON转模型
这里我就不做简单的演示了(因为难的我也不会),大家可以看看别人的三方库,里面用到了runtime的东西。
总结
大家还有什么好的runtime使用情况,可以留言或者私信给我,我就添加在文章上面,供给大家看。欢迎吐槽!!!
网友评论