在Swift中,完成后是否应将我的可选实例变量设置为nil? [英] In Swift should I set my optional instance variables to nil when done?

查看:604
本文介绍了在Swift中,完成后是否应将我的可选实例变量设置为nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中一个应用程序中使用了很多音频,视频和图像,并且似乎存在较小的内存问题,并且想知道释放内存的最佳方法是什么.

I'm using lots of audio, video, and images in one of my apps and seem to be having minor memory issues and was wondering what the best way to free up memory is.

我使用许多这样的可选变量:

I use lots of optional variables like this:

var myImageView: UIImageView?

我想知道是否最好的方法是将这些设置为nil,一旦您知道不再需要它来释放内存,就这样:

I was wondering if it is considered best practice to set these to nil as soon as you know you won't need it anymore to free up memory like this:

myImageView = nil

似乎将其设置为nil会删除最后一个强引用并导致其被释放,但如果可能的话,我也不希望在各处乱码XXXX = nil.

It seems like setting it to nil would remove the last strong reference and cause it to be released, but I would also prefer not to litter my code with XXXX = nil all over the place if possible.

我还考虑过为使用该变量的类创建一个deinit方法,并在其中进行如下操作:

I also thought about creating a deinit method for the class that uses this variable and do it in there like this:

deinit {
    myImageView = nil
}

唯一的事情是我正在使用的实例在再次使用之前实际上并没有被销毁.但是通常在实例被销毁时,它的所有可选对象也应该被释放,对吧?

The only thing is the instance I'm using doesn't actually get destroyed before it's used again. But normally when an instance is destroyed, all of it's optionals should be released as well, right?

推荐答案

来自Apple的文档关于自动引用计数(ARC),他们说:

From Apple's documentation on Automatic Reference Counting (ARC), they say:

Swift使用自动引用计数(ARC)来跟踪和管理您的应用的内存使用情况.在大多数情况下,这意味着内存管理在Swift中有效",您无需自己考虑内存管理.当不再需要类实例使用的实例时,ARC会自动释放它们.

Swift uses Automatic Reference Counting (ARC) to track and manage your app’s memory usage. In most cases, this means that memory management "just works" in Swift, and you do not need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed.

以下部分对您来说很有趣

The following part seems interesting to you

但是,在某些情况下,ARC需要有关以下方面的更多信息: 您的代码部分之间的关​​系,以便为以下内容管理内存 你.

However, in a few cases ARC requires more information about the relationships between parts of your code in order to manage memory for you.

您还没有发布任何代码,所以我不知道您是否有弱引用,无主引用,强闭包引用循环等.

You haven't posted any code, so I can't know whether you have weak references, unowned references, strong reference cycles for closures etc.

例如,如果您有一个强大的关闭参考周期,如上面的文档链接所述:

For example, if you have a strong reference cycle for closure as described in the documentation link above:

在使用类类型的属性时,Swift提供了两种解决强引用周期的方法:弱引用和无主引用.

Swift provides two ways to resolve strong reference cycles when you work with properties of class type: weak references and unowned references.

我认为阅读文档对您很有帮助,因为它可以使您清楚地了解ARC在Swift中的工作方式.

I think it would be beneficial for you to read the documentation as it will give you a clear idea of how ARC works in Swift.

这篇关于在Swift中,完成后是否应将我的可选实例变量设置为nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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