成功与失败合在一起
#import <Foundation/Foundation.h>
@class EOCNetworkFetcher;
typedef void (^EOCNetworkFetcherCompletionHandler)(NSData *data,NSError *error);
@interface EOCNetworkFetcher : NSObject
- (id)initWithURL:(NSURL*)URL;
- (void)startWithCompletionHandler:(EOCNetworkFetcherCompletionHandler)handler;
@end
//调用:
- (void)fetchFooData{
NSURL *url = [[NSURL alloc] initWithString:@""];
_fooFetcher = [[EOCNetworkFetcher alloc] initWithURL:url];
[_fooFetcher startWithCompletionHandler:^(NSData *data,NSError *error){
if(error){
}else{
_fetchedFooData = data;
}
}];
}
成功与失败分开
#import <Foundation/Foundation.h>
@class EOCNetworkFetcher;
typedef void (^EOCNetworkFetcherCompletionHandler)(NSData *data);
typedef void (^EOCNetworkFetcherErrorHandler)(NSError *error);
@interface EOCNetworkFetcher : NSObject
- (id)initWithURL:(NSURL*)URL;
- (void)startWithCompletionHandler:(EOCNetworkFetcherCompletionHandler)completion
failureHandler:(EOCNetworkFetcherErrorHandler)failure;
@end
//调用:
- (void)fetchFooData{
NSURL *url = [[NSURL alloc] initWithString:@""];
_fooFetcher = [[EOCNetworkFetcher alloc] initWithURL:url];
[_fooFetcher startWithCompletionHandler:^(NSData *data){
_fetchedFooData = data;
}
failureHandler:^(NSError *error){
}];
}
网友评论