首先看下面这段代码:
void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){ NSLog(@"====%d",theOne+theTwo); }; myFirstBlock(100,300);
个人理解:block本身就是一个类,他的实例化就是一个对象,下面看看这个数据请求的封装,来好好理解这句话。
我把数据请求这一款,封装在了:WebService这个类中,先看h文件:
<span style="font-size:14px;">// // WebService.h // BlockDemo // // Created by junyuan ji on 14-6-3. // Copyright (c) 2014年 junyuan ji. All rights reserved. // #import <Foundation/Foundation.h> typedef void (^successBlock)(NSMutableURLRequest * request,NSMutableData * reciveData); typedef void (^errorBlock)(NSMutableURLRequest * request); typedef void (^startBlock)(NSMutableURLRequest * request); typedef void (^complectionBlock)(NSMutableURLRequest * request,NSMutableData * reciveData); @interface WebService : NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate> { __block NSHTTPURLResponse * httpResponse; } @property (nonatomic,copy) successBlock successblock; @property (nonatomic,copy) errorBlock errorblock; @property (nonatomic,copy) startBlock startblock; @property (nonatomic,copy) complectionBlock complectionblock; @property (nonatomic,retain) __block NSMutableData * reciveMutableData; -(void)StartUrl:(NSString *)path; -(void)Url:(NSString *)path onSuccess:(successBlock)successBlock onError:(errorBlock)errorBlock onStart:(startBlock)startBlock onCompletion:(complectionBlock)complectionBlock; @end</span><span style="font-size:18px;"> </span>
下面看看m文件的代码:
// // WebService.m // BlockDemo // // Created by junyuan ji on 14-6-3. // Copyright (c) 2014年 junyuan ji. All rights reserved. // #import "WebService.h" @implementation WebService -(void)Url:(NSString *)path onSuccess:(successBlock)successBlock onError:(errorBlock)errorBlock onStart:(startBlock)startBlock onCompletion:(complectionBlock)complectionBlock { [self StartUrl:path]; self.successblock = ^(NSMutableURLRequest * request,NSMutableData * reciveData){ if (httpResponse.statusCode == 200) { successBlock(request,reciveData); } }; self.startblock = ^(NSMutableURLRequest *request){ startBlock(request); }; self.errorblock = ^(NSMutableURLRequest * request){ errorBlock(request); }; self.complectionblock = ^(NSMutableURLRequest * request,NSMutableData * reciveData){ complectionBlock(request,reciveData); }; } -(void)StartUrl:(NSString *)path { NSURL * url = [NSURL URLWithString:path]; NSMutableURLRequest * request1 = [NSMutableURLRequest requestWithURL:url]; [NSURLConnection connectionWithRequest:request1 delegate:self]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%s",__FUNCTION__); self.errorblock((NSMutableURLRequest *)connection.currentRequest); } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"%s",__FUNCTION__); httpResponse = (NSHTTPURLResponse *)response; self.reciveMutableData = [NSMutableData dataWithCapacity:0]; self.startblock((NSMutableURLRequest *)connection.currentRequest); } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"%s",__FUNCTION__); [self.reciveMutableData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"%s",__FUNCTION__); self.complectionblock((NSMutableURLRequest *)connection.currentRequest,self.reciveMutableData); } @end
下面看看如何使用封装的这个类:
#import "AppDelegate.h" #import "WebService.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){ NSLog(@"====%d",theOne+theTwo); }; myFirstBlock(100,300); [[[WebService alloc] init] Url:@"http://zhangmenshiting.baidu.com/data2/music/65532488/1490719597200128.mp3?xcode=f1ea4f162f276d435fc963a294370657c9d1e761819ce1d5" onSuccess:^(NSMutableURLRequest *request, NSMutableData *reciveData) { NSLog(@"reciveData = %@",reciveData); } onError:^(NSMutableURLRequest *request) { NSLog(@"error = "); } onStart:^(NSMutableURLRequest *request) { NSLog(@"start"); } onCompletion:^(NSMutableURLRequest *request, NSMutableData *reciveData) { NSLog(@"completion"); }]; return YES; }
Block学习一:基本用法,用block封装NSURLRequest异步请求,布布扣,bubuko.com
Block学习一:基本用法,用block封装NSURLRequest异步请求
原文地址:http://blog.csdn.net/jijunyuan/article/details/28414031