1、block是什么?block是一个特殊的OC对象,它建立在栈上, 而不是堆上, 这么做一个是为性能考虑,还有就是方便访问局部变量.
#import "ViewController.h"
typedef void(^done1)(NSString *message);
typedef int(^done2)(NSString *message);
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// [self loadHttpRequest:^(NSString *message)
// {
// NSLog(@"在控件上显示配置数据:%@",message);
// }];
//
// [self loadHttpRequest_1:^int(NSString *message) {
// NSLog(@"%d",(int)[message integerValue]);
//
// return (int)[message integerValue];
//
// }];
[self loadHttpRequest:^(NSString *message) {
NSLog(@"在控件上显示配置数据:%@",message);
}];
[self loadHttpRequest1:^int(NSString *message) {
NSLog(@"%d",(int)[message integerValue]);
return (int)[message integerValue];
}];
}
//省略block名字
//- (void)loadHttpRequest:(void(^)(NSString *message))completion
//{
// //假设加载出来的数据
// NSString *data = @"1";
// //回调,去配置数据
// completion(data);
//}
//省略block名字
//- (void)loadHttpRequest_1:(int(^)(NSString *message))completion
//{
// //假设加载出来的数据
// NSString *data = @"1";
// //回调,去配置数据
// completion(data);
//}
//定义block名字
- (void)loadHttpRequest:(done1)completion
{
//假设加载出来的数据
NSString *data = @"1";
//回调,去配置数据
completion(data);
}
//定义block名字
- (void)loadHttpRequest1:(done2)completion
{
//假设加载出来的数据
NSString *data = @"1";
//回调,去配置数据
completion(data);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end