“-(void)encodeWithCoder:(NSCoder *)aCoder{
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
const char *cName = ivar_getName(ivarList[i]);
NSString *name = [NSString stringWithUTF8String:cName];
[aCoder encodeObject:[self valueForKey:name] forKey:name];
}
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
const char *cName = ivar_getName(ivarList[i]);
NSString *name = [NSString stringWithUTF8String:cName];
[self setValue:[aDecoder decodeObjectForKey:name] forKey:name];
}
}
return self;
}
/*
Ivar *class_copyIvarList(Class cls, unsigned int *outCount) //获取所有成员变量
const char *ivar_getName(Ivar v) //获取某个成员变量的名字
const char *ivar_getTypeEncoding(Ivar v) //获取某个成员变量的类型编码
Ivar class_getInstanceVariable(Class cls, const char *name) //获取某个类中指定名称的成员变量
id object_getIvar(id obj, Ivar ivar) //获取某个对象中的某个成员变量的值
void object_setIvar(id obj, Ivar ivar, id value) //设置某个对象的某个成员变量的值
TypeEncoding:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1
*/
-(NSString *)description{
/** class:要获取的某个类, outCount:通过一个函数执行之后会将成员变量的数值赋值到此 */
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
//每次获取一个成员变量
Ivar ivar = ivarList[i];
//打印成员变量的名字和类型编码
NSLog(@"name = %s, type = %s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
}
return nil;
}
+(Person *)personWithName:(NSString *)name gender:(NSString *)gender age:(NSNumber *)age weight:(NSInteger)weight
{
Person *person = [Person new];
unsigned int outCount;
Ivar *ivarList = class_copyIvarList(self, &outCount);
// obj:要设置的对象 ivar:要设置的对象的某一个属性 value:value
object_setIvar(person, ivarList[0], name);
object_setIvar(person, ivarList[1], gender);
object_setIvar(person, ivarList[2], age);
object_setIvar(person, ivarList[3], @(weight));
return person;
}
-(void)getPersonMessage
{
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
NSLog(@"name = %s, value = %@", ivar_getName(ivarList[i]), object_getIvar(self, ivarList[i]));
}
}
/*
1 + resolveInstanceMethod:(SEL)sel // 为一个实例方法动态添加实现
+ resolveClassMethod:(SEL)sel // 为一个类方法动态添加实现
2 - (id)forwardingTargetForSelector:(SEL)aSelector
//为没有实现的方法指定一个对象
3 - (void)forwardInvocation:(NSInvocation *)anInvocation
//子类重载这个方法为消息指定其他对象
*/
//消息转化1:为一个实例方法动态添加实现
+ (BOOL)resolveInstanceMethod:(SEL)sel{
NSString *selString = NSStringFromSelector(sel);
if ([selString isEqualToString:@"wolkOnTheStreet:"]) {
// 为一个没有实现的方法添加实现
/** cls:类
name:没有实现的方法
IMP:要添加的方法
types:动态添加的实现的类型编码
*/
class_addMethod(self, @selector(wolkOnTheStreet:), (IMP)wolkFunc, "V@:@");
}
return [super resolveInstanceMethod:sel];
}
void wolkFunc(id self,SEL sel, NSString *str){
NSLog(@"Person---%s---%@", __func__,str);
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
NSString *selString = NSStringFromSelector(aSelector);
if ([selString isEqualToString:@"wolkOnTheStreet:"]) {
self.dog = [Dog new];
return self.dog;
}
return [super forwardingTargetForSelector:aSelector];
}
//切换消息转化对象:二
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([Dog instancesRespondToSelector:anInvocation.selector]) {
self.dog = [Dog new];
[anInvocation invokeWithTarget:self.dog];
}
}
//给方法制定一个有效的签名
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *methodSignature = [super methodSignatureForSelector:aSelector];
if (!methodSignature) {
methodSignature = [Dog instanceMethodSignatureForSelector:aSelector];
}
return methodSignature;
}”
runtime的简述
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
- OC最实用的runtime总结,面试、工作你看我就足够了! 前言runtime的资料网上有很多了,部分有些晦涩难懂...
- 把 OS X 自带的 mount_ntfs 默认加载方式从只读改成读写, 具体方法如下(# 开头的是注释,其他是需...