iOS-将块传递给函数 [英] iOS - Passing blocks to functions

查看:93
本文介绍了iOS-将块传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法调用(这是来自AFNetworking的调用):

I have a method call (it's a call from AFNetworking):

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                     {
                                         NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);

                                     } failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
                                     {
                                         NSLog(@"Failed: %@",[error localizedDescription]);
                                     }];

,我正在尝试将成功和失败块拉到一个单独的变量中,以便稍后将其传递给该方法.但是我不知道如何将块声明为变量.我正在寻找做这样的事情:

and I'm trying to pull the sucess and failure blocks out into a separate variable, that I can later pass into the method. But I can't figure out how to declare the blocks as variables. I'm looking to do something like this:

IDontKnowWhatKindOfDataTypeGoesHere successBlock = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
  NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);  
}

和failureBlock相同.

and the same for the failureBlock.

因此,我正在寻找拨打AFJSONRequestOperation的电话,例如:

So then I'm looking to make the AFJSONRequestOperation call like:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:successBlock failure:failureBlock];

但是我无法确定successBlock和failureBlock的数据类型应该是什么.

But I cant figure how what the data types of the successBlock and failureBlock should be.

我想这更适合组织.我的successBlock中有很多代码,Xcode推送的自动格式化位于屏幕的右侧,这非常令人讨厌.因此,如果我可以将其拔出(应该是正确的),那么我可以更好地组织代码.

This is more for organization I suppose. I have a lot of code in my successBlock and the autoformatting of Xcode pushes is all to the right side of the screen which is totally annoying. So if I can pull it out (which should be possible, right), then I can organize my code better.

谢谢!

推荐答案

除非您习惯了它,否则它会很尴尬.变量名称与类型...混在一起出现.

It's awkward until you get used to it. The variable name appears mixed in with the type...

void (^success)(NSURLRequest *, NSHTTPURLResponse *, id) = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
   NSLog(@"request is %@, response is %@ and json is %@", request, response, JSON);
};

在这种情况下,堆栈变量名称为成功.现在,您可以在具有相同类型的后续表达式中引用它,例如...

The stack variable name in this case is success. You can now refer to it in subsequent expressions that take the same type, like ...

AFJSONRequestOperation *operation = [AFJSONRequestOperationJSONRequestOperationWithRequest:request
                                        success:success];

您还可以将块设置为属性,如下所示:

You can also make a block an property, like this:

@property (copy, nonatomic) void (^success)(NSURLRequest *, NSHTTPURLResponse *, id);

并这样称呼它:

self.success(aRequest, aResponse, someJSON);

请记住在完成调用后将其清除,因此调用者不必担心创建保留周期.

Remember to nil it out when you're done calling it, so the caller has less to worry about creating a retain cycle.

编辑:在评论中有关使用typedef使其在眼睛和手指上更容易使用的好建议:

Edit: good suggestion in commentary about using typedef to make this easier on the eyes and fingers:

typedef void (^SuccesBlock)(NSURLRequest *, NSHTTPURLResponse *, id);

所以堆栈变量看起来像这样:

So the stack variable looks like this:

SuccessBlock success = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
   NSLog(@"request is %@, response is %@ and json is %@", request, response, JSON);
};

,该属性如下所示:

@property (copy, nonatomic) SuccessBlock success;

这篇关于iOS-将块传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆