OC高效率52之通过委托与数据源协议进行对象间通信

#import <Foundation/Foundation.h>
// 对象间通信:
// 在信息源模式中,信息又数据源流向类
// 普通的委托模式中,信息则从类流向受委托者
@class EOCNetworkFetcher;

@protocol EOCNetworkFetcherDelegate <NSObject>

@required
-(void)networkFetcher:(EOCNetworkFetcher *)fetcher didReceiveData:(NSData *)data;

@optional
-(void)networkFetcher:(EOCNetworkFetcher *)fetcher didFailWithError:(NSError *)error;
@end



@interface EOCNetworkFetcher : NSObject

@property (nonatomic , weak) id<EOCNetworkFetcherDelegate> delegate;//利用属相来存放委托对象
@end

#import "EOCNetworkFetcher.h"

@implementation EOCNetworkFetcher
-(void)didSomeSthing{

    if ([_delegate respondsToSelector:@selector(networkFetcher:didReceiveData:)]){
        [_delegate networkFetcher:nil didReceiveData:nil];
    }
}
@end
#import <Foundation/Foundation.h>
#import "EOCNetworkFetcher.h"
@interface EOCDataModel : NSObject<EOCNetworkFetcherDelegate>

@end

#import "EOCDataModel.h"

@implementation EOCDataModel
-(void)networkFetcher:(EOCNetworkFetcher *)fetcher didReceiveData:(NSData *)data
{
//do some things
}
-(void)networkFetcher:(EOCNetworkFetcher *)fetcher didFailWithError:(NSError *)error
{

}
@end


你可能感兴趣的:(OC高效率52之通过委托与数据源协议进行对象间通信)