Swift - 以编程方式创建视图时的 Lazy Var 与 Let(节省内存) [英] Swift - Lazy Var vs. Let when creating views programmatically (saving memory)

查看:53
本文介绍了Swift - 以编程方式创建视图时的 Lazy Var 与 Let(节省内存)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,我有点了解 Lazy Var 与 Let.我注意到它在使用 Lazy Var 尤其是 ImageViews 时节省了大量内存使用量.但是到目前为止我看到的教程和指南并没有经常使用 Lazy Var,所以我怀疑这是一种不好的做法并且我忽略了一些东西.

我做了一些研究,了解到 Lazy 不是线程安全的",但我不明白这是什么意思.我看到了很多利弊,但我无法得出任何结论,尤其是因为我的知识非常有限.

在创建 UIView 时,什么时候可以(或更好)使用 Lazy Var 和 Let?

lazy var profileImageView: UIImageView = {让 imageView = UIImageView(image: #imageLiteral(resourceName: "page1"))imageView.translatesAutoresizingMaskIntoConstraints = falseimageView.contentMode = .scaleAspectFit返回图像视图}()

解决方案

是否使用 lazy var 取决于您的代码及其上下文.它本身并没有坏或好.你必须决定什么时候合适.

在你做出决定之前,你必须知道什么是lazy var.

什么是lazy var?

延迟初始化是一个概念,其中变量内容的初始化(构造)被延迟到第一次使用.第一次访问此类变量会触发初始化.由于在使用(需要)变量之前不会创建内容,因此使用惰性初始化变量可以节省资源.

这是延迟初始化背后的主要驱动力.除非你需要它,否则你不会创造它.这也是您在决定是否应该lazy var 时使用的逻辑.

如果您正在处理始终可见(需要)的视图(或其他任何东西),那么使用延迟初始化就没有什么意义了.另一方面,当您处理并不总是需要的实例时 - 那么使用 lazy var 是合理的.

如果您的视图在呈现的视图控制器中始终可见,那么通过使其变得懒惰您将无法完成太多工作.如果它仅在特定情况下可见 - 例如当用户展开一些折叠的面板时 - 那么让它变得懒惰是有意义的.默认情况下,它将使您的视图控制器加载速度更快并使用更少的内存.

<小时>

就线程安全而言,lazy var 在 Swift 中不是线程安全的.

这意味着如果两个不同的线程试图同时访问同一个lazy var,那么在初始化此类变量之前,其中一个线程可能会访问部分构造的实例.>

您可以在以下位置找到有关线程安全的更多信息:

Swift - 惰性 var 线程安全吗?

使lazy var"线程安全

I'm a beginner and I sort of understand Lazy Var vs. Let. I've noticed that it saves a ton of memory usage when using Lazy Var especially with ImageViews. But the tutorials and guides I've seen so far don't use Lazy Var very often, so I'm feeling suspicious that it is bad practice and that I'm overlooking something.

I did a little research and learned that Lazy isn't "thread safe," but I don't understand what this means. I've seen a lot of pros and cons, but I can't draw any conclusions especially because I have very limited knowledge.

When is it ok (or better) to use Lazy Var vs. Let when creating a UIView?

lazy var profileImageView: UIImageView = {

    let imageView = UIImageView(image: #imageLiteral(resourceName: "page1"))
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFit
    return imageView

}()

解决方案

Whether you will use lazy var or not depends on your code and its context. It is not bad or good on its own. You have to decide when it is appropriate.

Before you can decide that, you have to know what lazy var is.

What is lazy var?

Lazy initialization is a concept where initialization (construction) of variable content is delayed until its first usage. First access to such variable triggers initialization. Since content is not created until variable is used (needed) using lazy initialized variables can save resources.

That is primary drive behind lazy initialization. You don't create something until you need it. That is also logic you will use when deciding whether something should be lazy var or not.

If you are dealing with views (or anything else) that are always visible (needed) there is little point in using lazy initialization. On the other hand when you are dealing with instances that are not always needed - then using lazy var is justified.

If your view is always visible in presented view controller, you will not accomplish much by making it lazy. If it is visible only under specific circumstances - for instance when user expands some collapsed panel - then making it lazy makes sense. It will make your view controller load faster and use less memory by default.


As far as thread safety is concerned, lazy var are not thread safe in Swift.

That means if two different threads try to access the same lazy var at the same time, before such variable has been initialized it is possible that one of the threads will access partially constructed instance.

You can find more about thread safety in:

Swift - is lazy var thread-safe?

Make "lazy var" threadsafe

这篇关于Swift - 以编程方式创建视图时的 Lazy Var 与 Let(节省内存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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