iOS runtime研究

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,758评论 0 9
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,230评论 0 7
  • 本文转载自:http://yulingtianxia.com/blog/2014/11/05/objective-...
    ant_flex阅读 776评论 0 1
  • 参考链接: http://www.cnblogs.com/ioshe/p/5489086.html 简介 Runt...
    乐乐的简书阅读 2,153评论 0 9
  • 转载:http://yulingtianxia.com/blog/2014/11/05/objective-c-r...
    F麦子阅读 760评论 0 2