onevcat / OneV-s-Den-Comments

0 stars 0 forks source link

2020/03/improve-delegate/ #12

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

使用 protocol 和 callAsFunction 改进 Delegate | OneV's Den

2018 年 3 月的时候我写过一篇在 Swift 中如何改进 Delegate Pattern 的文章,主要思想是用遮蔽变量 (shadow variable) 声明的方式,来保证 self 变量可以被常时地标记为 weak。本文中,为了保证没有看过原文的读者能处在同一频道,我会先 (再次) 简单介绍一下这种方法。然后,结合 Swif

https://onevcat.com/2020/03/improve-delegate/

xiaoyouPrince commented 3 years ago

本来是一个简单的代码规范。一个非常基础的 [weak self]. 完全可以人为规定此类代码的写法。

为了解决/优化 [weak self] 的问题,反而引入了更多的“人为规定”的额外的工具代码。

Delegate 工具的使用,确实人为规定了新的规范,通过代理转移了 [weak self] 的位置,但同样引入了新的依赖,对新人不友好,“不直观” 的新问题。我个人人为反而是最基础的 [weak self] 更通用,更直击问题。

yanglfree commented 3 years ago

好熟悉的内容,Kingfisher里面的Delegate就是这么处理的,😄

USTBER commented 2 years ago

请教一个问题,如果Input是Void,只能通过: onConfirmInput(())来调用么? 总感觉这样写有一些奇怪。

onevcat commented 2 years ago

整理和改进了一下,现在放在这里了:https://github.com/onevcat/Delegate

对于 Void 参数,并不需要再显示括号,直接 onConfirmInput() 就行了。

awesomejue commented 1 year ago

I prefer this