美文网首页
iOS runtime研究

iOS runtime研究

作者: crazyfox | 来源:发表于2017-02-24 15:28 被阅读162次

structobjc_class {

Class isaOBJC_ISA_AVAILABILITY;

#if !__OBJC2__

Class super_classOBJC2_UNAVAILABLE;//父类

constchar*nameOBJC2_UNAVAILABLE;//类名

longversionOBJC2_UNAVAILABLE;//版本号,默认0

longinfoOBJC2_UNAVAILABLE;//类信息

longinstance_sizeOBJC2_UNAVAILABLE;//类实例变量大小

structobjc_ivar_list *ivarsOBJC2_UNAVAILABLE;//属性列表

structobjc_method_list **methodListsOBJC2_UNAVAILABLE;//方法列表

structobjc_cache *cacheOBJC2_UNAVAILABLE;//经常调用的方法列表

structobjc_protocol_list *protocolsOBJC2_UNAVAILABLE;//协议列表

#endif

}OBJC2_UNAVAILABLE;

方法

-(void)test

{

//获取属性列表

unsignedintcount;

objc_property_t*propertyList =class_copyPropertyList([selfclass], &count);

for(NSIntegeri =0;i<count;i++)

{

constchar*property =property_getName(propertyList[i]);

NSLog(@"%@",[NSStringstringWithUTF8String:property]);

}

objc_property_tt =class_getProperty([selfclass],"str4");

//获取方法列表

Method*methodList =class_copyMethodList([selfclass], &count);

for(NSIntegeri =0;i<count;i++)

{

Methodmethod = methodList[i];

NSLog(@"%@",NSStringFromSelector(method_getName(method)));

}

//获取成员变量列表

Ivar*ivarList =class_copyIvarList([selfclass], &count);

for(NSIntegeri =0;i<count;i++)

{

Ivarivar = ivarList[i];

NSLog(@"%@",[NSStringstringWithUTF8String:ivar_getName(ivar)]);

}

//获取协议列表

__unsafe_unretainedProtocol **protocolList =class_copyProtocolList([selfclass], &count);

for(NSIntegeri =0;i<count;i++)

{

Ivarivar = ivarList[i];

NSLog(@"%@",[NSStringstringWithUTF8String:ivar_getName(ivar)]);

}

}

方法调用

消息发送过程,objc_msgsend(id,SEL,var...)

首先在对象的类对象的chche,methodList以及父类对象的cache,methondList里寻找SEL对应的IMP

如果没有找到,实现了方法//动态决议方法

+ (BOOL)resolveClassMethod:(SEL)name; 

 + (BOOL)resolveInstanceMethod:(SEL)name;

并且在里面提供了相应的SEL,那就能正确运行,否则

跳到//消息转发函数  让别的对象去执行这个函数

- (id)forwardingTargetForSelector:(SEL)aSelector

如果未实现//将目标函数以其他形式执行

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

- (void)forwardInvocation:(NSInvocation *)anInvocation

否则到

- (void)doesNotRecognizeSelector:(SEL)aSelector

crash

简单例子

voiddynamicMethodIMP(idself,SEL_cmd)

{

printf("SEL %s did not exist\n",sel_getName(_cmd));

}

//动态方法解析   动态加方法

+(BOOL)resolveInstanceMethod:(SEL)sel

{

//if(sel==@selector(some))

//{

//class_addMethod([self class],sel,(IMP)dynamicMethodIMP,"v@:");

//return YES;

//

//}

return[superresolveInstanceMethod:sel];;

}

//转交给其他类处理

-(id)forwardingTargetForSelector:(SEL)aSelector

{

//if(aSelector==@selector(some))

//{

//

//return [ViewController new];

//}

returnnil;

}

//完整消息转发机制

- (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector {

NSMethodSignature*sig;

sig = [[ViewControllernew]methodSignatureForSelector:aSelector];

returnsig;

}

-(void)forwardInvocation:(NSInvocation*)anInvocation

{

idtarget = [ViewControllernew];

[anInvocationinvokeWithTarget:target];

}

SEL : 类成员方法的指针,但不同于C语言中的函数指针,函数指针直接保存了方法的地址,但SEL只是方法编号。

IMP:一个函数指针,保存了方法的地址

关联对象

NSString*something =@"111";

UILabel*label = [UILabelnew];

objc_setAssociatedObject(something, &someKey, label,OBJC_ASSOCIATION_RETAIN);

UILabel*label1 =objc_getAssociatedObject(something, &someKey);

方法交换 method swizzling  

void method_exchangeImplementations(Method m1, Method m2)

//放在load里是因为load只触发一次,用dispatch_once也可以

+(void)load

{

MethodfromMethod =class_getInstanceMethod(self,@selector(viewDidLoad));

MethodtoMethod =class_getInstanceMethod(self,@selector(changeMethod));

if(!class_addMethod([selfclass],@selector(viewDidLoad),method_getImplementation(toMethod),method_getTypeEncoding(toMethod)))

{

method_exchangeImplementations(fromMethod,toMethod);

}

}

//这边[self changeMethod];其实调用的是[self viewDidLoad];

-(void)changeMethod

{

NSLog(@"changeMethod");

[self changeMethod];

}

框架:JRSwizzle

参考http://www.cocoachina.com/ios/20150901/13173.html

相关文章

网友评论

      本文标题:iOS runtime研究

      本文链接:https://www.haomeiwen.com/subject/rrebwttx.html