swift 嵌套函数_Swift中的嵌套函数。 真实经验

swift 嵌套函数

I want to give you a clear understanding of nested functions and share my experience in using nested functions in Swift.

我想让您清楚地了解嵌套函数,并分享我在Swift中使用嵌套函数的经验。

理论部分 (Theoretical Part)

Swift Functions can have nested functions inside their bodies. Nested functions must be defined before the body of their master function.

Swift 函数可以在其主体内部具有嵌套函数。 嵌套函数必须在其主函数主体之前定义。

Nested functions are accessible only in their master function. (Only inside runReportFlow() you can call getWeekReportBody() and getMonthReportBody()).

嵌套函数只能在其主函数中访问。 (仅在runReportFlow()内部,您可以调用getWeekReportBody()getMonthReportBody() )。

陷阱! (Pitfalls!)

Reference cycle and memory leaks knowledge in Swift is very useful here. For example, in the code below, the nested function will have a strong reference on Car object.

Swift中的参考周期和内存泄漏知识在这里非常有用。 例如,在下面的代码中,嵌套函数将对Car对象具有强大的引用。

Functions are very similar to Closures, but they have differences. One of them is the Capture List. The nested function doesn’t have [weak self] list, and that’s why it is easiest to write buggy code with strong references.

功能与闭包非常相似,但有区别。 其中之一是捕获列表 。 嵌套函数没有[weak self]列表,这就是为什么最容易编写具有强引用的错误代码的原因。

第一部分。 强大的参考 (First Part. Strong reference)

So let’s look at the first part of the code above. We have 2 cases. The car calls a strong function where self is strongly captured by DispatchQueue closure. As a result, we have that the Factory will be deallocated immediately and after 3 secs Car will call showModel() function and will be deallocated too. And it is a problem. So if we want to delete and deallocate factory and car immediately (without some unexpected functions call for example, like showModel()), that we need to rewrite it with a custom weak reference like in part 2.

因此,让我们看一下上面代码的第一部分。 我们有2个案例。 汽车调用了一个强大的功能,即DispatchQueue关闭功能强烈地捕获了self 。 结果,我们不得不立即将Factory释放,并在3秒钟后Car将调用showModel() 功能,也将被释放。 这是一个问题。 因此,如果我们想立即删除并重新分配工厂和汽车(例如,没有一些意外的函数调用,例如showModel() ),则需要使用自定义的weak引用(如第2部分)重写它。

第二部分。 参考不足 (Second Part. Weak reference)

In the second part, we need to create a custom weak reference.

在第二部分中,我们需要创建一个自定义的弱引用。

weak var _self = self. As a result, we have that the Factory and Car will be deallocated immediately and after 3 secs this code will be called _self?.showModel(), but _self is nil because it has a weak reference to Car object.

weak var _self = self 。 结果,我们不得不立即将Factory和Car释放,并在3秒钟后将此代码称为_self?.showModel() ,但是_selfnil因为它对Car对象的引用很弱。

实践部分 (Practical Part)

I clearly understand how to use nested functions and how to resolve strong reference problems here, but I didn’t understand why it is useful to use. I have 5 years of experience in commercial programming development and I think that nested functions are very niche and useless and you can simply avoid using them. You just need to understand how to use them and which problems they have (because you should easy to work with not your nested functions, that was written by someone else), but it is useless to use them by themselves. As for me, they have huge problems and limitations. The main problems are reusability. It is short-sighted to think that these nested functions will be needed only in this master function. What if tomorrow you will need some nested function in another place? Duplicate or refactor — it is your choice. A secondary problem for me is readability. I think that master function with 3 nested functions has poor readability and maintainability in the future. It looks pretty messy as for me.

我清楚地了解了如何使用嵌套函数以及如何在此处解决强引用问题,但是我不明白为什么使用嵌套函数很有用。 我在商业程序开发方面有5年的经验,并且我认为嵌套函数非常特殊且无用,您可以避免使用它们。 您只需要了解如何使用它们以及它们有哪些问题(因为您应该易于使用而不是别人编写的嵌套函数),但是单独使用它们是没有用的。 对于我来说,它们有巨大的问题和局限性。 主要问题是可重用性。 短视认为这些嵌套函数仅在该主函数中才需要。 如果明天您将需要在另一个地方使用嵌套函数,该怎么办? 复制或重构-这是您的选择。 对我来说,第二个问题是可读性。 我认为具有3个嵌套函数的主函数在将来的可读性和可维护性很差。 对我来说,看起来很乱。

结论 (Conclusions)

  • Nested functions are a function inside a master function

    嵌套函数是主函数内部的函数
  • Nested functions must be defined before the master body code

    嵌套函数必须在主体代码之前定义
  • Without knowledge about weak references, you can easily create unwanted strong references and memory leaks

    在不了解弱引用的情况下,您可以轻松创建不需要的强引用和内存泄漏
  • Try to avoid nested functions in most cases. I think that the nested functions can be useful in some cases — but it is hard to imagine these cases and it is better to avoid them at all

    在大多数情况下,尽量避免使用嵌套函数。 我认为嵌套函数在某些情况下可能很有用-但是很难想象这些情况,最好完全避免使用它们
  • Nested functions can have duplications and refactoring problems if they will be needed in some other places

    如果在其他一些地方需要嵌套函数,则可能存在重复和重构问题
  • Nested functions have readability and maintainability problems

    嵌套函数具有可读性和可维护性问题

翻译自: https://medium.com/swlh/nested-functions-in-swift-real-experience-88cf00301f0e

swift 嵌套函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值