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