block做方法参数时--block的参数传值过程 例1
说明:此例子中方法的调用在此文中是从下到上调用的。(即: 方法五调用方法四; 方法四调用方法三)
方法一:
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
// completionBlock is manually nilled out in AFURLConnectionOperation to break the retain cycle.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
#pragma clang diagnostic ignored "-Wgnu"
self.completionBlock = ^{
if (self.completionGroup) {
dispatch_group_enter(self.completionGroup);
}
dispatch_async(http_request_operation_processing_queue(), ^{
if (self.error) {
if (failure) {
dispatch_group_async(self.completionGroup ?: http_request_operation_completion_group(), self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(self, self.error);
});
}
} else {
id responseObject = self.responseObject; // self.responseObject 是本方法所在文件中的一个属性
if (self.error) {
if (failure) {
dispatch_group_async(self.completionGroup ?: http_request_operation_completion_group(), self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(self, self.error);
});
}
} else {
if (success) {
dispatch_group_async(self.completionGroup ?: http_request_operation_completion_group(), self.completionQueue ?: dispatch_get_main_queue(), ^{
success(self, responseObject);
});
}
}
}
if (self.completionGroup) {
dispatch_group_leave(self.completionGroup);
}
});
};
#pragma clang diagnostic pop
}
方法二、
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = self.responseSerializer;
operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
operation.credential = self.credential;
operation.securityPolicy = self.securityPolicy;
[operation setCompletionBlockWithSuccess:success failure:failure];
operation.completionQueue = self.completionQueue;
operation.completionGroup = self.completionGroup;
return operation;
}
方法三、
- (AFHTTPRequestOperation *)HTTPRequestOperationWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSError *serializationError = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
if (serializationError) {
if (failure) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
#pragma clang diagnostic pop
}
return nil;
}
return [self HTTPRequestOperationWithRequest:request success:success failure:failure];
}
方法四、
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"POST" URLString:URLString parameters:parameters success:success failure:failure];
[self.operationQueue addOperation:operation];
return operation;
}
方法五、 注:typedef void(^HttpRequestSuccess)(HDXHttpRequestManager* manager,id model);
#pragma mark - 食府搜索
-(void)RestaurantListWithKeyWord:(NSString *)keyword Lat:(CGFloat)lat Lng:(CGFloat)lng Success:(HttpRequestSuccess)success Fail:(HttpRequestFail)fail
{
NSDictionary *dic=@{@"keyword":keyword,@"lat":[NSNumber numberWithFloat:lat],@"lng":[NSNumber numberWithFloat:lng]};
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
[manager POST:[BaseURL stringByAppendingString:@"foods/list.json"] parameters:[self getParams:dic] success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(self,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
fail(self, error);
}];
}
方法六、
-(void)SearchReastaurant
{
HDXHttpRequestManager *manager=[HDXHttpRequestManager shareManager];
[manager RestaurantListWithKeyWord:_searchBar.text Lat:Lat Lng:Lng Success:^(HDXHttpRequestManager *manager, id model) {
DLog(@"====搜索出来的几家食府=数据=%@",model);
if ([model[@"code"] isEqualToNumber:@(200)]) {
[mapview removeAnnotations:annotationAry];
[annotationAry removeAllObjects];
for (NSDictionary *dic in model[@"data"]) {
ModelRestaurantList *listModel=[[ModelRestaurantList alloc]initWithDictionary:dic];
HDXPointAnnotation *annotation=[[HDXPointAnnotation alloc] init];
annotation.title=listModel.restaurantName;
// annotation.subtitle=listModel.address;
annotation.coordinate=CLLocationCoordinate2DMake(listModel.lat, listModel.lng);
annotation.ID=listModel.ID;
[annotationAry addObject:annotation];
}
if (annotationAry.count>0) {
[mapview addAnnotations:annotationAry];
}
else{
[MBProgressHUD showMessage:@"对不起,没有找到您需要的食府" toView:nil];
}
}
} Fail:^(HDXHttpRequestManager *manager, id model) {
DLog(@"%@",model);
}];
}
相当于 A 调 B、 B 调用 C、C 调用 D。
在 A 里面调用 B 的时候顺便 实现 B 方法的 block 回调方法 ( B方法参数其中有一个是一个block类型的参数 ),B方法里面会调用 B 方法的参数的block变量 (相当于拿到函数指针做调用)
block做方法参数时--block的参数传值过程 例1的更多相关文章
- OC中block作方法参数时的用法
方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name para1:(int)temp1 para2:(int)temp2 suc ...
- OC中的block作方法参数时的用法
方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name success:(int (^)(int param1, int para ...
- block 做参数
三部分 1,定义函数 /* 传出类定义block */ //定义block typedef void (^ItemClickBlock)(NSInteger selectedIndex); //blo ...
- Block作为参数时的使用
Block作为参数使用,常见于各框架之中,比如在封装一个类时,当做什么事情由外界去决定,什么时候调用由自己的类决定时,这时候就需要将block作为参数使用. 下面我们模仿AFNetworking的ma ...
- C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
一直以为对于引用类型做为参数在方法调用时加不加 ref 关键字是没有区别的.但是今天一调试踪了一下变量内存情况才发现大有不同. 直接上代码,结论是:以下代码是使用了 ref 关键字的版本.它输出1 ...
- ssm的web项目,浏览器使用get方法传递中文参数时,出现乱码
ssm的web项目,浏览器使用get链接传递的为中文参数时,出现乱码 做搜索功能时,搜索手机,那么浏览器传递的参数为中文参数“手机”,但传递的默认编码格式为iso-8859-1,所以传到后台时,是乱码 ...
- 优化MySQL开启skip-name-resolve参数时显示“ignored in --skip-name-resolve mode.”Warning解决方法
转自:http://blog.csdn.net/yiluoak_47/article/details/53381282 参数用途: skip-name-resolve #禁止MySQL对外部连接进行D ...
- Mybatis:解决调用带有集合类型形参的mapper方法时,集合参数为空或null的问题
此文章有问题,待修改! 使用Mybatis时,有时需要批量增删改查,这时就要向mapper方法中传入集合类型(List或Set)参数,下面是一个示例. // 该文件不完整,只展现关键部分 @Mappe ...
- 【Java学习笔记之二十七】Java8中传多个参数时的方法
java中传参数时,在类型后面跟"..."的使用: public static void main(String[] args){ testStringA ...
随机推荐
- ruby -- 进阶学习(一)subdomain配置与实现
今天和guanMac童鞋研究的subdomain配置终于有点头绪~~ 之所以会遇到种种难题,个人总结了一下,第一本人太菜,第二英语不好 贴一下guanMac童鞋配置小结的链接:http://my.eo ...
- 介绍WEB站点结构
在这节里,我们将抛开Umbraco来看看已创建的站点.在我们介绍Umbraco之前,需要了解站点是如何工作的,如何使用使用浏览器工具. 我们看到在标签顶端的内容叫做页面标题.每个页面的标题都会改变表示 ...
- gopush-cluster 架构
前言 gopush-cluster是一套golang开发的实时消息推送集群,主要分享一下开发这套系统的想法和思路. 架构 主要分为三个模块来开发,comet/web/message. comet 主要 ...
- sql server 查找包含字符串的对象
sql server 查找包含字符串的对象 SELECT sm.object_id, OBJECT_NAME(sm.object_id) AS object_name, o.type, o.type_ ...
- WCF回顾一、基本概念和应用场景
一.WCF描述 wcf是一款基于面向服务的架构的通讯框架平台,在分布式框架中得到了广泛使用. wcf入门非常简单,只要花几分钟就能编写一个完整的wcf程序,而实际上WCF是概念非常多的一门技术,需要花 ...
- HTML5使用ApplicationCache
在html5中使用application cache可以把一些静态资源保存在客户端的浏览器上面.这样可以提高访问的速度,甚至是离线应用.关于application cache的优缺点:1.离线浏览 - ...
- win8.1安装开发工具vs2013.3+mssql2012全程
几个常用的命令 重起计算机命令:shoutdown.exe -r -t 0 立刻重起 在远程桌面中没有关机重起的选项,这个命令是必须的 远程桌面连接:mstsc 硬件环境:I7 4770 64RAM ...
- C#实用杂记-EF全性能优化技巧2
原文链接: http://www.cnblogs.com/zhaopei/p/5721789.html
- MVC WebApi跨域ajax接受post数据笔记
后端api代码示例: [HttpPost] public string callbackUrl([FromBody]SZRCallBackModel cbm) { try { if (cbm == n ...
- 用C#开发的双色球走势图(一)
首先声明,个人纯粹无聊之作,不作商业用途. 我相信每个人都拥有一个梦想那就是有朝一日能中500W,这个也一直是我的梦想,并默默每一期双色球或多或少要贡献自己一点点力量,本人并不属于那种铁杆的彩票迷,每 ...