iOS-代理优化

2017-06-28  本文已影响0人  黑酒一
@class EOCNetworkingFetcher;

struct {
    unsigned int didReceiveData : 1;
    unsigned int didFailWirhError : 1;
    unsigned int didUpdateProgressTo : 1;
} _delegateFlags;

@protocol EOCNetworkingFetcherDelegate <NSObject>

@optional
- (void)newworkingFetcher:(EOCNetworkingFetcher *)fetcher
           didRecevieData:(NSData *)data;

@end

@interface EOCNetworkingFetcher : NSObject

@property (nonatomic, weak) id <EOCNetworkingFetcherDelegate> delegate;

@end
if ([self.delegate respondsToSelector:@selector(newworkingFetcher:didRecevieData:)]) {
    NSData *data;
    [self.delegate newworkingFetcher:self didRecevieData:data];
}
位段,C语言允许在一个结构体中以位为单位来指定其成员所占内存长度,这种以位为单位的成员称为“位段”或称“位域”( bit field) 。
struct data {
unsigned int filedA : 8;
unsigned int filedB : 4;
unsigned int filedC : 2;
unsigned int filedD : 1;
}
利用位段就可以清楚的表示delegate 对象是否能响应协议中的方法。
struct {
    unsigned int didReceiveData : 1;
    unsigned int didFailWirhError : 1;
    unsigned int didUpdateProgressTo : 1;
} _delegateFlags;
_delegateFlags.didReceiveData = 1;
if (_delegateFlags.didReceiveData) {
    // yes
    NSData *data;
    [self.delegate newworkingFetcher:self didRecevieData:data];
}else {
    // no
    if ([self.delegate respondsToSelector:@selector(newworkingFetcher:didRecevieData:)]) {

    }
}
上一篇下一篇

猜你喜欢

热点阅读