首页 > 代码库 > Block学习一:基本用法,用block封装NSURLRequest异步请求
Block学习一:基本用法,用block封装NSURLRequest异步请求
首先看下面这段代码:
void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){ NSLog(@"====%d",theOne+theTwo); }; myFirstBlock(100,300);
void:指的时返回数据类型,myFirstBlock:指的是block的名称,后面两个是参数。等号右边的block的实现。
个人理解: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 one rror:(errorBlock)errorBlock onStart:(startBlock)startBlock onCompletion:(complectionBlock)complectionBlock; @end</span><span style="font-size:18px;"> </span>
上面typedef是声明block,下面把block声明称属性,比如successblock是一个对象,自然可以把它声明成属性。
下面看看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 one rror:(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
上面这几段代码,应该好好去理解,如果上面这段代码看懂了,block基本上也就掌握了。
下面看看如何使用封装的这个类:
#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 = http://www.mamicode.com/%@",reciveData);>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。