11 协议

认识协议

这里写图片描述

//Drawable.h 文件
#import <Foundation/Foundation.h>

@protocol Drawable

@property  NSInteger x;
@property  NSInteger y;

-(void)draw;
+(void)createShape;
//-(id)init;
//-(void)dealloc;

@optional
-(void)moveToX:(NSInteger)x withY:(NSInteger)y;

@end

使用协议

这里写图片描述

只出现警告信息而不是错误,是因为有运行时添加方法的机制。所以编译器会认为这样也未必就是错误。

#import <Foundation/Foundation.h>
#import "Drawable.h"


@interface BLNPoint : NSObject<Drawable> // 用<>准守协议

@property  NSInteger x; 
@property  NSInteger y; 
// 协议的属性也可以不在头文件声明,直接自己创建实例变量和getter/setter方法。但是很少如此使用。

@end
// 作为类型约束使用
void process1(id<Drawable> obj)
{
    [obj draw];
    NSLog(@"[%ld,%ld]",(long)obj.x,(long)obj.y);
}

void process2(id obj){
    if ([obj conformsToProtocol:@protocol(AProtocol) ]) {
        [obj methodA];
    }
}

void process3(id<BProtocol> obj)
{
    [obj methodA];
    [obj methodB];
}
// - (BOOL)conformsToProtocol:(Protocol *)aProtocol;
if ([obj conformsToProtocol:@protocol(AProtocol) ]) {
    [obj methodA];
}

更多协议模式

这里写图片描述

// -------------     定义协议    -------------
@protocol AProtocol

-(void)methodA;

@end

@protocol BProtocol <AProtocol>

-(void)methodB;

@end

@protocol CProtocol

-(void)methodC;

@end

@protocol Protocol
// 默认会有一个@require。加了@optional之后,在再次遇到@require之前都会是可选性的。
@require
-(void)methodD;
@optional 
-(void)moveToX:(NSInteger)x withY:(NSInteger)y;
@end
// -------------     遵守协议的类    -------------

@interface ClassA : NSObject<AProtocol>

@end

@interface ClassB : NSObject<BProtocol>

@end

@interface ClassC : NSObject<AProtocol,CProtocol>

@end

常用协议

这里写图片描述

@protocol NSObject

- (BOOL)isEqual:(id)object;
@property (readonly) NSUInteger hash;

@property (readonly) Class superclass;
- (Class)class OBJC_SWIFT_UNAVAILABLE("use 'anObject.dynamicType' instead");
- (instancetype)self;

- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

- (BOOL)isProxy;

- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;

- (BOOL)respondsToSelector:(SEL)aSelector;

- (instancetype)retain OBJC_ARC_UNAVAILABLE;
- (oneway void)release OBJC_ARC_UNAVAILABLE;
- (instancetype)autorelease OBJC_ARC_UNAVAILABLE;
- (NSUInteger)retainCount OBJC_ARC_UNAVAILABLE;

- (struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;

@property (readonly, copy) NSString *description;
@optional
@property (readonly, copy) NSString *debugDescription;

@end
@protocol NSCopying

- (id)copyWithZone:(nullable NSZone *)zone;

@end
@protocol NSMutableCopying

- (id)mutableCopyWithZone:(nullable NSZone *)zone;

@end
@protocol NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER

@end
@protocol NSFastEnumeration

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len;

@end

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值