定义
#import <Foundation/Foundation.h>
@class EOCNetworkFetcher;
@protocol EOCNetworkFetcherDelegate<NSObject>
- (void)networkFetcher:(EOCNetworkFetcher *)nerworkFetcher
didFinishWithData:(NSData *)data;
@end
@interface EOCNetworkFetcher : NSObject
@property(nonatomic,weak) id <EOCNetworkFetcherDelegate> delegate;
- (id)initWithURL:(NSURL*)URL;
- (void)start;
@end
其他类可以这样使用
//设置代理部分
- (void)fetchFooData{
NSURL *url = [[NSURL alloc] initWithString:@""];
_fooFetcher = [[EOCNetworkFetcher alloc] initWithURL:url];
_fooFetcher.delegate = self;
[_fooFetcher start];
}
- (void)fetchBarData{
NSURL *url = [[NSURL alloc] initWithString:@""];
_barFetcher = [[EOCNetworkFetcher alloc] initWithURL:url];
_barFetcher.delegate = self;
[_barFetcher start];
}
//实现代理部分
- (void)networkFetcher:(EOCNetworkFetcher *)nerworkFetcher
didFinishWithData:(NSData *)data{
if(networkFetcher == _fooFetcher){
_fetchedFooData = data;
_fooFetcher = nil;
}else{
_fetchedBarData = data;
_barFetcher = nil;
}
}