方法成员
类方法成员有instance method(-)和class method(+),分别存储于class object和meta-class object,存储在class object(meta-class object)的methodLists中
struct objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
struct objc_method_list {
struct objc_method_list *obsolete OBJC2_UNAVAILABLE;
int method_count OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
/* variable length structure */
struct objc_method method_list[1] OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
/// An opaque type that represents a method in a class definition.
typedef struct objc_method *Method;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
分析:
- 方法成员结构为objc_method,objc_method保存着方法成员名(method_name),方法成员类型(method_types),方法成员指针(method_imp),方法成员名唯一确定方法成员
- objc_method_list*是方法成员list,因此同一方法成员list中,方法成员名不能重名,否则编译器不能确定使用哪个方法成员,二义性错误,或者list中优先search到的方法成员屏蔽list中之后重名方法成员(ignore之后重名方法成员),oc在编译期避免此情况发生
- methodLists类型为objc_method_list**,即方法成员list的list,methodLists(后缀s,表示复数,多个objc_method_list*)为list,其node类型为objc_method_list*(方法成员list),methodLists之所以使用objc_method_list**类型是因为类支持类别(category),primary class和每个类别(category)的方法成员list用一个objc_method_list*(方法成员list)表示,作为methodLists的一个node
SEL&IMP&Method
SEL,IMP,Method是oc(特别是runtime)中经常使用的数据类型,跟方法成员结构objc_method紧密相关
/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;
/// A pointer to the function of a method implementation.
#if !OBJC_OLD_DISPATCH_PROTOTYPES
typedef void (*IMP)(void /* id, SEL, ... */ );
#else
typedef id (*IMP)(id, SEL, ...);
#endif
/// An opaque type that represents a method in a class definition.
typedef struct objc_method *Method;
总结:
- 方法成员签名,唯一确定方法成员,方法成员签名包括默号(:),默号(:)个数即为参数个数,因此feed和feed:表示不同方法成员签名,前者无参,后者带1个参数
- SEL类型通过@selctor(方法成员签名)获取,sel_getName(SEL sel)获取SEL的方法成员签名(字符串形式,const char*)
- IMP为方法成员指针类型,即函数指针类型,指向方法成员入口地址
- Method为方法成员结构指针,保存着方法成员名(method_name),方法成员类型(method_types),方法成员指针(method_imp),方法成员名唯一确定方法成员
- (void)method_signature
{
SEL sel1 = @selector(feed);
SEL sel2 = @selector(feed:);
SEL sel3 = @selector(feedRice:);
SEL sel4 = @selector(feedRice:andMeat:);
const char* sel1_name = sel_getName(sel1);
const char* sel2_name = sel_getName(sel2);
const char* sel3_name = sel_getName(sel3);
const char* sel4_name = sel_getName(sel4);
NSLog(@"sel1_name = %s", sel1_name);
NSLog(@"sel2_name = %s", sel2_name);
NSLog(@"sel3_name = %s", sel3_name);
NSLog(@"sel4_name = %s", sel4_name);
}
output:
sel1_name = feed
sel2_name = feed:
sel3_name = feedRice:
sel4_name = feedRice:andMeat:
方法成员调用原理
方法成员调用根据SEL在methodLists中search Method,如果found,通过Method的IMP调用方法成员,如果not found,抛出异常或crash
SEL是Method标识符,唯一确定Method,在同一primary class或同一类别(category)中不能含SEL相同的多个Method声明或定义,因为根据SEL在methodLists中一旦找到Method,search terminate(如果还有相同SEL的Method,ignored),因此声明或定义SEL相同的多个Method无意义,oc在编译期避免此情况发生
注:interface中含SEL相同的多个Method声明,若参数类型一致,编译warning,若参数类型不一致,编译error,implementation中含SEL相同的多个Method定义,编译error
应用
instance method(-)和class method(+)分别存储于class object和meta-class object,因此instance method(-)和class method(+)即使SEL相同也互不影响
@interface FBAnimal : NSObject
- (void)feedRice:(int)rice andMeat:(int)meat;
+ (void)feedRice:(int)rice andMeat:(int)meat;
@end
@implementation FBAnimal
- (void)feedRice:(int)rice andMeat:(int)meat
{
NSLog(@"instance method(-) feedRice:andMeat:");
}
+ (void)feedRice:(int)rice andMeat:(int)meat
{
NSLog(@"class method(+) feedRice:andMeat:");
}
@end
同一方法成员list(同一primary class或同一类别)中,SEL相同的多个Method声明或定义,编译warning或编译error(interface中含SEL相同的多个Method声明,若参数类型一致,编译warning,若参数类型不一致,编译error,implementation中含SEL相同的多个Method定义,编译error)
@interface FBFood : NSObject
@end
@implementation FBFood
@end
@interface FBAnimal : NSObject
- (void)feedRice:(int)rice andMeat:(int)meat;
- (void)feedRice:(int)rice andMeat:(FBFood *)meat;
@end
@implementation FBAnimal
- (void)feedRice:(int)rice andMeat:(int)meat
{
NSLog(@"instance method(-) feedRice:andMeat:");
}
- (void)feedRice:(int)rice andMeat:(FBFood *)meat
{
NSLog(@"instance method(-) feedRice:andMeat:");
}
@end
同一方法成员list(同一primary class或同一类别)中,SEL不同的多个Method,函数定义(函数体)相同,编译器认为是不同Method(这只是设计上的问题,代码冗余),因为SEL唯一确定方法成员
@interface FBAnimal : NSObject
- (void)feedRice:(int)rice andMeat:(int)meat;
- (void)feedRice:(int)rice meat:(int)meat;
@end
@implementation FBAnimal
- (void)feedRice:(int)rice andMeat:(int)meat
{
//same code
}
- (void)feedRice:(int)rice meat:(int)meat
{
//same code
}
@end