runtime——Class——协议

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值