说明:此例子中方法的调用在此文中是从下到上调用的。(即:     方法五调用方法四;      方法四调用方法三)

方法一:
- (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的更多相关文章

  1. OC中block作方法参数时的用法

    方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name para1:(int)temp1 para2:(int)temp2 suc ...

  2. OC中的block作方法参数时的用法

    方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name success:(int (^)(int param1, int para ...

  3. block 做参数

    三部分 1,定义函数 /* 传出类定义block */ //定义block typedef void (^ItemClickBlock)(NSInteger selectedIndex); //blo ...

  4. Block作为参数时的使用

    Block作为参数使用,常见于各框架之中,比如在封装一个类时,当做什么事情由外界去决定,什么时候调用由自己的类决定时,这时候就需要将block作为参数使用. 下面我们模仿AFNetworking的ma ...

  5. C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处

    ​ 一直以为对于引用类型做为参数在方法调用时加不加 ref 关键字是没有区别的.但是今天一调试踪了一下变量内存情况才发现大有不同. 直接上代码,结论是:以下代码是使用了 ref 关键字的版本.它输出1 ...

  6. ssm的web项目,浏览器使用get方法传递中文参数时,出现乱码

    ssm的web项目,浏览器使用get链接传递的为中文参数时,出现乱码 做搜索功能时,搜索手机,那么浏览器传递的参数为中文参数“手机”,但传递的默认编码格式为iso-8859-1,所以传到后台时,是乱码 ...

  7. 优化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 ...

  8. Mybatis:解决调用带有集合类型形参的mapper方法时,集合参数为空或null的问题

    此文章有问题,待修改! 使用Mybatis时,有时需要批量增删改查,这时就要向mapper方法中传入集合类型(List或Set)参数,下面是一个示例. // 该文件不完整,只展现关键部分 @Mappe ...

  9. 【Java学习笔记之二十七】Java8中传多个参数时的方法

    java中传参数时,在类型后面跟"..."的使用:        public static void main(String[] args){       testStringA ...

随机推荐

  1. 爬虫技术 -- 进阶学习(九)使用HtmlAgilityPack获取页面链接(附c#代码及插件下载)

    菜鸟HtmlAgilityPack初体验...弱弱的代码... Html Agility Pack是一个开源项目,为网页提供了标准的DOM API和XPath导航.使用WebBrowser和HttpW ...

  2. Tips7:Unity中 Scene视图 和 Game视图 中 视角(Camera)的控制

    选中你要改变的相机,然后点击GameObject-->Align With View 选项(快捷键Ctrl+Shift+F)使相机视角和当前Sence视图中一样 通过这样可以控制在Game视图( ...

  3. 剑指架构师系列-Struts2的缓存

    Struts2的缓存中最重要的两个类就是ReferenceMap与ReferenceCache.下面来解释下ReferenceCache中的get()方法. public V get(final Ob ...

  4. Linux的段错误调试方法

    linux段错误的调试方法 相关博文: http://blog.csdn.net/htianlong/article/details/7439030 http://www.cnblogs.com/pa ...

  5. 通过python切换hosts文件

    做开发或测试时常需要切换hosts ,如果hosts比较多,那么频繁的打开hosts文件对地址加注释(#),再把去掉注释是个繁琐的事情. 当然,SwitchHosts 已经可以帮我们方便的解决了这个繁 ...

  6. 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-9系统,3行代码即可集成.

    提示:如果你发现了Bug,请尝试更新到最新版.目前最新版是1.6.4,此前的版本或多或少存在一些bug的~如果你已经是最新版了,请留一条评论,我看到了会尽快处理和修复哈~ 关于升级iOS10和Xcdo ...

  7. Angular系列----AngularJS入门教程01:AngularJS模板 (转载)

    是时候给这些网页来点动态特性了——用AngularJS!我们这里为后面要加入的控制器添加了一个测试. 一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模 ...

  8. 使用Spark分析拉勾网招聘信息(二): 获取数据

    要获取什么样的数据? 我们要获取的数据,是指那些公开的,可以轻易地获取地数据.如果你有完整的数据集,肯定是极好的,但一般都很难通过还算正当的方式轻易获取.单就本系列文章要研究的实时招聘信息来讲,能获取 ...

  9. WatiN和HttpWatch交互简介

    Httpwatch是一款强大的网页数据分析工具,它可以在不改变浏览器和网络设置的基础上捕捉http和https数据.查看底层的http数据,包括headers, cookies, cache等,同时统 ...

  10. js获取url参数的两种方法

    js获取参数,在以前我都是用正在去拆分,然后获取,这种方式感觉是最简单的 方式1: function QueryString(item) { var sValue=location.search.ma ...