zoukankan      html  css  js  c++  java
  • 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变量 (相当于拿到函数指针做调用)

  • 相关阅读:
    Postman:Pre-request Script
    PHP修改脚本最大执行时间和最大内存限制
    PHP函数:array_chunk
    PHP出现SSL certificate:unable to get local issuer certificate的解决办法
    Linux命令:chown
    PHP函数:fopen
    PHP常量:JSON_UNESCAPED_UNICODE
    Android -- 贝塞尔曲线公式的推导和简单使用
    Java -- 浅入Java反射机制
    Java -- 深入浅出GC自动回收机制
  • 原文地址:https://www.cnblogs.com/nxz-diy/p/5287966.html
Copyright © 2011-2022 走看看