菜鸟教程小白 发表于 2022-12-12 15:16:48

ios - RACDelegateProxy 与 Common Delegate 实现


                                            <p><p>我正在研究 ReactiveCocoa(我不擅长 FRP,也不擅长纯函数式编程),并且我看到了 RacDelegateProxy 类,它可以抽象具有 void 返回值的委托(delegate)方法的实现。我不太明白的是使用它有什么好处,当我们与传统的实现进行比较时,即实现每个方法并获得调用。那么,使用 RACDelegateProxy 而不是以传统方式实现的主要区别和好处是什么?</p>

<p>代码示例(使用 ReactiveCocoa):</p>

<pre><code>self.deselectDelegate = [ initWithProtocol:@protocol(UITableViewDelegate)];
[
subscribeNext:^(RACTuple *arguments) {
   // code ...
}];
</code></pre>

<p>VS(传统实现):</p>

<pre><code>- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    // code ...
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您的问题可以概括为“信号相对于回调的优势是什么”。</p>
<p>以前在 <a href="https://github.com/ReactiveCocoa/ReactiveCocoa" rel="noreferrer noopener nofollow">RAC&#39;s github repo</a> 上有一个链接称为 <strong>Escape from Callback Hell</strong> - 链接不再有效,但我认为该标题完美地突出了这一点。</p>
<p>基本上,FRP 的优点之一是将所有异步模式(KVO、委托(delegate)、通知等)转换为信号,这会导致:</p>
<ul>
<li><p><strong>简单组合</strong>:您是否尝试过链接多个网络操作?这对标准方法来说是相当麻烦的。您甚至可能需要在异步链中添加一些数据库操作或位置更新...</p>
</li>
<li><p><strong>提高可读性和更容易维护</strong>:相关代码在同一个地方,而不是以回调的形式遍布整个类。</p>
</li>
</ul>
<hr/>
<h2>编辑</h2>
<p>发现提到的文章已存档:<a href="https://web.archive.org/web/20140716025555/http://elm-lang.org/learn/Escape-from-Callback-Hell.elm" rel="noreferrer noopener nofollow">Escape from Callback Hell</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - RACDelegateProxy 与 Common Delegate 实现,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/30193633/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/30193633/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - RACDelegateProxy 与 Common Delegate 实现