1.block进行声明定义以变量的形式使用在代码中
->声明一个Block
//声明一个square的Block Pointer,其所指向的Block有一个int输入和int输出
int (^square)(int);
//将Block实体指定给square
square = ^(int a){ return a*a ; };
//调用方法,感觉是是不是很像function的用法?
int result = square(5);
-》定义一个block
int (^mySquare)(int) = ^(int a){return a*a;}; //定义一个mySquare的Block pointer变量
->block作为函数的参数,在oc编程中block的名可以去掉。
-(void)objcMethod:(int(^)(int))square; //square参数的类型是int(^)(int)
void myFunction(int (^mySquare)(int)); //function的定义,将Block作为参数
int (^mySquare)(int) = ^(int a){return a*a;}; //定义一个mySquare的Block pointer变量
myFunction(mySquare); //把mySquare作为myFunction的参数
》》》》》通过 类型重定义 函数书写更加简洁。block的名可以等效于block数据类型。
#if NS_BLOCKS_AVAILABLE
typedefNSComparisonResult (^NSComparator)(id obj1,id obj2);
#endif
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
等价于:
- (NSArray *)sortedArrayUsingComparator:(NSComparisonResult(^)(id obj1,id obj2))cmptr
2.iOS中使用block进行网络请求回调
原文:http://www.tuicool.com/articles/JFRfmq
PS:简单的使用block进行网络请求回调,非常的方便,避免了delegate的繁琐
HttpRequest.h
#import <Foundation/Foundation.h>
typedef void (^FinishBlock)(NSString *dataString);
@interface HttpRequest : NSObject<NSURLConnectionDataDelegate>
@property (strong, nonatomic) NSMutableData *resultData;
@property (strong, nonatomic) FinishBlock finishBlock;
+ (void)postRequestWithURL:(NSString *)urlStr
paramters:(NSString *)paramters
finshedBlock:(FinishBlock)block;
@end
#import "HttpRequest.h"
@implementation HttpRequest
+ (void)postRequestWithURL:(NSString *)urlStr
paramters:(NSString *)paramters
finshedBlock:(FinishBlock)block
{
HttpRequest *httpRequest = [[HttpRequest alloc]init];
httpRequest.finishBlock = block;
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *requset = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
[requset setHTTPBody:[paramters dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:requset delegate:httpRequest];
NSLog(connection ? @"连接创建成功" : @"连接创建失败");
}
/**
* 接收到服务器回应的时回调
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (!self.resultData) {
self.resultData = [[NSMutableData alloc]init];
} else {
[self.resultData setLength:0];
}
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dic = [httpResponse allHeaderFields];
NSLog(@"[network]allHeaderFields:%@",[dic description]);
}
}
/**
* 接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.resultData appendData:data];
}
/**
* 数据传完之后调用此方法
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *resultStr = [[NSString alloc]initWithData:self.resultData
encoding:NSUTF8StringEncoding];
if (self.finishBlock) {
self.finishBlock(resultStr);
}
}
/**
* 网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"network error : %@", [error localizedDescription]);
if (self.finishBlock) {
self.finishBlock(nil);
}
}
@end
在ViewController中调用:
[HttpRequest postRequestWithURL:@"http://www.baidu.com"
paramters:@""
finshedBlock:^(NSString *dataString) {
NSLog(@"finish callback block, result: %@", dataString);
}];