此处模拟一下封装网络请求类,使用block向外面抛值
首先写网络请求工具类
+(void)networkRequestWithBlock:(void(^)(NSString *))callBack{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"-----发送异步网络请求");
dispatch_async(dispatch_get_main_queue(), ^{
callBack(@"------jisonData");
});
});
然后在控制器中,调用工具类,使用block向外面抛请求得到的结果
#import "ViewController.h"
#import "NetworkRequestTool.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[NetworkRequestTool networkRequestWithBlock:^(NSString *jisonData) {
NSLog(@"------%@",jisonData);
}];
}