如何将基于代理的回调系统转换为基于块的? [英] How to convert a delegate-based callback system into block-based?

查看:180
本文介绍了如何将基于代理的回调系统转换为基于块的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有一个基于代理的系统来发送不同类型的请求。当请求完成时,它使用委托来告诉对象,并且如果它是一个成功的错误。



现在,我还必须检查是什么类型的请求它是响应采取适当的行动。



我有包装类,应该给我一个基于块的界面相同。



我将一个完成块和一个错误块传递给应该在内部使用这个基于代理的类的请求方法。



当响应到来时,应该自动调用该请求类型的相应处理程序,并取决于成功和错误。



我在SO上看到类似的问题,但对我来说有点不清楚,所以请给出一个如何去做的总体思路,而不是将其直接标记为重复。

解决方案

这是一种方式去做吧。使用此 RAExpendable 类可以使用基于块的实施动态构建代理。 p>

我们假设你的代表是:

  @protocol XDelegate 
- (void)foo:(id)response;
@end

https://github.com/evadne/RAExpendable 到您的项目。动态添加委托方法:

  RAExpendable *消耗品= [RAExpendable new]; 
[可用addMethodForSelector:@selector(foo :) types:v @:@block:^(id x,SEL sel,id response){
NSLog(@response is%@响应);
}]

并将可消耗的类设置为您的代理:

  someObject.delegate = consumable; 

现在,如果你这样做:

  [可用的performSelector:@selector(foo :) withObject:@OK]; 

您得到的字符串响应是OK 。将NSLog替换为您认为合适的任何成功/失败实现。从现在开始,当您调用 foo:时,块将执行。



如果要修改此选项您的用例,请注意,此示例的参数为 v @:@ ,根据类型编码运行时指南意味着:void return,self,SEL,object。 self SEL 是每个Objective-C方法上存在的两个隐藏参数,第三个参数是第一个非隐藏方法的参数。块的签名必须与方法的签名相匹配。


I have a class, which has a delegate based system for sending different type of requests. it uses delegate to tell the object when the request is complete and also if it was a success o an error.

Now, I also have to check what type of request was it in response to take appropriate action.

I have wrapper class that should give me a block based interface for the same.

I pass a completion-block and an error-block to a request method which should internally use this delegate based class.

And when the response comes, should automatically call the appropriate handler for that request type and depending on success and error as well.

I saw a similar question on SO, but it was a little unclear to me, So please give a general idea of how to go about it instead of marking it as duplicate straight away.

解决方案

Here is one way to do it. Use this RAExpendable class to dynamically build a delegate with a block based implementation.

Let's say your delegate is:

@protocol XDelegate
-(void) foo:(id)response;
@end

Add RAExpendable.h, RAExpendable.m from https://github.com/evadne/RAExpendable to your project. Dynamically add the delegate method:

    RAExpendable *expendable = [RAExpendable new];
    [expendable addMethodForSelector:@selector(foo:) types:"v@:@" block:^(id x, SEL sel, id response){
        NSLog(@"response is %@", response);
    }];

And set the expendable class as your delegate:

    someObject.delegate = expendable;

Now, if you do this:

    [expendable performSelector:@selector(foo:) withObject:@"OK"];

You get the string response is OK. Replace NSLog with whatever success/failure implementation you see fit. From now on, when you call foo:, the block executes instead.

If you want to modify this for your use case, note that the parameters for this example were v@:@, which according to the Type Encoding guide of the runtime means: void return, self, SEL, object. self and SEL are the two hidden parameters present on every Objective-C methods, the third parameter is the first non hidden parameter of the method. The signature of the block has to match the signature of the method.

这篇关于如何将基于代理的回调系统转换为基于块的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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