您的位置:首页 > 其它

block做方法参数时--block的参数传值过程 例1

2016-03-17 16:37 211 查看
说明:此例子中方法的调用在此文中是从下到上调用的。(即: 方法五调用方法四; 方法四调用方法三)

方法一:
- (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变量 (相当于拿到函数指针做调用)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: