runtime的簡(jiǎn)述

“-(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)? ? ? ? ? ? //獲取某個(gè)成員變量的名字

const char *ivar_getTypeEncoding(Ivar v)? //獲取某個(gè)成員變量的類(lèi)型編碼

Ivar class_getInstanceVariable(Class cls, const char *name)? ? //獲取某個(gè)類(lèi)中指定名稱(chēng)的成員變量

id object_getIvar(id obj, Ivar ivar)? ? //獲取某個(gè)對(duì)象中的某個(gè)成員變量的值

void object_setIvar(id obj, Ivar ivar, id value)? ? //設(shè)置某個(gè)對(duì)象的某個(gè)成員變量的值

TypeEncoding:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1

*/

-(NSString *)description{

/** class:要獲取的某個(gè)類(lèi), outCount:通過(guò)一個(gè)函數(shù)執(zhí)行之后會(huì)將成員變量的數(shù)值賦值到此 */

unsigned int outCount;

Ivar *ivarList = class_copyIvarList([Person class], &outCount);

for (NSInteger i = 0; i < outCount; i++) {

//每次獲取一個(gè)成員變量

Ivar ivar = ivarList[i];

//打印成員變量的名字和類(lè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:要設(shè)置的對(duì)象 ivar:要設(shè)置的對(duì)象的某一個(gè)屬性 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? ? ? // 為一個(gè)實(shí)例方法動(dòng)態(tài)添加實(shí)現(xiàn)

+ resolveClassMethod:(SEL)sel? ? ? //? 為一個(gè)類(lèi)方法動(dòng)態(tài)添加實(shí)現(xiàn)

2? ? - (id)forwardingTargetForSelector:(SEL)aSelector

//為沒(méi)有實(shí)現(xiàn)的方法指定一個(gè)對(duì)象

3? ? - (void)forwardInvocation:(NSInvocation *)anInvocation

//子類(lèi)重載這個(gè)方法為消息指定其他對(duì)象

*/

//消息轉(zhuǎn)化1:為一個(gè)實(shí)例方法動(dòng)態(tài)添加實(shí)現(xiàn)

+ (BOOL)resolveInstanceMethod:(SEL)sel{

NSString *selString = NSStringFromSelector(sel);

if ([selString isEqualToString:@"wolkOnTheStreet:"]) {

// 為一個(gè)沒(méi)有實(shí)現(xiàn)的方法添加實(shí)現(xiàn)

/** cls:類(lèi)

name:沒(méi)有實(shí)現(xiàn)的方法

IMP:要添加的方法

types:動(dòng)態(tài)添加的實(shí)現(xiàn)的類(lèi)型編碼

*/

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];

}

//切換消息轉(zhuǎn)化對(duì)象:二

- (void)forwardInvocation:(NSInvocation *)anInvocation

{

if ([Dog instancesRespondToSelector:anInvocation.selector]) {

self.dog = [Dog new];

[anInvocation invokeWithTarget:self.dog];

}

}

//給方法制定一個(gè)有效的簽名

-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

{

NSMethodSignature *methodSignature = [super methodSignatureForSelector:aSelector];

if (!methodSignature) {

methodSignature = [Dog instanceMethodSignatureForSelector:aSelector];

}

return methodSignature;

}”

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容