objc_class&Class
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;
protocol API
Protocol
#ifdef __OBJC__
@class Protocol;
#else
typedef struct objc_object Protocol;
#endif
API
OBJC_EXPORT Protocol *objc_getProtocol(const char *name)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT Protocol * __unsafe_unretained *objc_copyProtocolList(unsigned int *outCount)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT Protocol *objc_allocateProtocol(const char *name)
__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
OBJC_EXPORT void objc_registerProtocol(Protocol *proto)
__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
runtime API
OBJC_EXPORT BOOL class_addProtocol(Class cls, Protocol *protocol)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT BOOL class_conformsToProtocol(Class cls, Protocol *protocol)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT Protocol * __unsafe_unretained *class_copyProtocolList(Class cls, unsigned int *outCount)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
注意:
- class_conformsToProtocol不会search super_class
- class_copyProtocolList不会search super_class
@protocol FBMovement
@required
- (void)run:(int)runSpeed;
- (void)swim:(int)swimSpeed;
- (void)fly:(int)flySpeed;
@end
@protocol FBEat
@required
- (void)eatRice;
@optional
- (void)eatMeat;
- (void)eatFish;
@end
@protocol FBAction <FBMovement, FBEat>
@optional
- (void)fun;
- (void)attack;
@end
@interface FBAnimal : NSObject <FBAction, FBEat>
{
@public
int _age;
int _color;
}
- (void)initAge:(int)age;
- (void)initAge:(int)age andColor:(int)color;
@end
@implementation FBAnimal
- (void)initAge:(int)age
{
}
- (void)initAge:(int)age andColor:(int)color
{
}
- (void)run:(int)runSpeed
{
}
- (void)swim:(int)swimSpeed
{
}
- (void)fly:(int)flySpeed
{
}
- (void)eatRice
{
}
@end
@protocol FBGuard
@required
- (void)guardHome;
@end
@interface FBDog : FBAnimal <FBGuard>
@end
@implementation FBDog
- (void)guardHome
{
}
@end
- (void)class_protocol
{
Protocol *FBAction = objc_getProtocol("FBAction");
Protocol *FBGuard = objc_getProtocol("FBGuard");
NSLog(@"FBDog conforms to protocol FBAction is = %d", class_conformsToProtocol([FBDog class], FBAction));
NSLog(@"FBDog conforms to protocol FBGuard is = %d", class_conformsToProtocol([FBDog class], FBGuard));
unsigned int protocolCnt = 0;
NSLog(@"animal protocol list");
__unsafe_unretained Protocol **animalProtocols = class_copyProtocolList([FBAnimal class], &protocolCnt);
for(int i = 0; i < protocolCnt; i++)
{
Protocol *thisProtocol = animalProtocols[i];
NSLog(@"protocol name = %s", protocol_getName(thisProtocol));
}
free(animalProtocols);
NSLog(@"dog protocol list");
__unsafe_unretained Protocol **dogProtocols = class_copyProtocolList([FBDog class], &protocolCnt);
for(int i = 0; i < protocolCnt; i++)
{
Protocol *thisProtocol = dogProtocols[i];
NSLog(@"protocol name = %s", protocol_getName(thisProtocol));
}
free(dogProtocols);
}
output:
FBDog conforms to protocol FBAction is = 0
FBDog conforms to protocol FBGuard is = 1
animal protocol list
protocol name = FBAction
protocol name = FBEat
dog protocol list
protocol name = FBGuard
总结:
- class_conformsToProtocol不会search super_class
- class_copyProtocolList不会search super_class,获取的protocol list只是直接遵守的protocol,不包括从基类继承的protocol和protocol继承的protocol