1.书写
#import <Foundation/Foundation.h>
@class EOCNetwork;
@protocol EOCNetworkFetcherDelegate<NSObject>
@optional
- (void)networkFetcher:(EOCNetwork *)fetcher
didReciveData:(NSData *)data;
- (void)networkFetcher:(EOCNetwork *)fetcher
didFailWithError:(NSData *)data;
@end
//1->1个二进制位,表示yes/no
struct {
unsigned int didReciveData :1;
unsigned int didFailWithError :1;
}_deleteFlags;
@interface EOCNetwork : NSObject
@property(nonatomic, weak)id<EOCNetworkFetcherDelegate> delegate;
@end
#import "EOCNetwork.h"
@implementation EOCNetwork
-(void)setDelegate:(id<EOCNetworkFetcherDelegate>)delegate
{
//只检测一次,避免了每次触发委托事件,都需要检测一次是否有响应
//如果是下载进度这种,就需要不断检测,过于浪费性能
_delegate = delegate;
_deleteFlags.didReciveData =[delegate respondsToSelector:@selector(networkFetcher:didReciveData:)];
_deleteFlags.didFailWithError = [delegate respondsToSelector:@selector(networkFetcher:didFailWithError:)];
}
-(void)didReceiveData
{
if (_deleteFlags.didReciveData) {
}
}
-(void)didFailWithError
{
if (_deleteFlags.didFailWithError) {
}
}
@end