Swift Lazy和Optional属性 [英] Swift Lazy and Optional properties

查看:204
本文介绍了Swift Lazy和Optional属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift中LazyOptional属性之间有什么区别?

What is the difference between a Lazy or Optional property in Swift?

例如,如果某人正在构建从侧面进入的导航栏,则我认为所有这些都应在一个UIViewController之内.用户可能永远不会打开菜单,但有时会打开.

For example, if someone is building a navigation bar that comes in from the side, I think that should all be within one UIViewController. The user might never open the menu but sometimes they will.

var menu: NavigationBar?
lazy var menu: NavigationBar = NavigationBar.initialize()

我认为这两个可选的代码都是不错的代码,因为除非需要,否则它们不会创建视图.我了解Optional表示可能有一个值,可能是nil.我也理解Lazy的意思是不用担心,直到我需要它为止.

Both of the optional I think are good code, because they don't create the view unless its needed. I understand Optional means there might be a value it might be nil. I also understand Lazy means don't worry about it until I need it.

特定问题

我的问题是,它们的性能模式(安全性和速度)是否表示可选件更快,更安全,反之亦然?

My question is are their performance patterns (safety and speed) that say optionals are faster and safer or vise versa?

推荐答案

好,这是一个有趣的问题,我不想暗示现有的答案不好,但我想我会提供承担一切.

OK, this is an interesting question, and I don't want to imply that the existing answers aren't good, but I thought I'd offer my take on things.

lazy变量非常适合需要先设置然后再重置的事情.它是一个变量,因此您可以将其更改为其他变量,但是这种做法违反了lazy变量的目的(即根据需要进行设置).

lazy variables are great for things that need to be setup once, then never re-set. It's a variable, so you could change it to be something else, but that kind of defeats the purpose of a lazy variable (which is to set itself up upon demand).

对于可能消失(并且可能再次出现)的事情,可选项更多.每次都需要设置它们.

Optionals are more for things that might go away (and might come back again). They need to be set up each time.

因此,让我们看一下侧边菜单的两种情况:一种在不可见的情况下停留在菜单周围,另一种在取消分配的情况下显示.

So let's look at two scenarios for your side menu: one where it stays around while it's not visible, and another for when it is deallocated.

lazy var sideMenu = SideMenu()

因此,第一次访问sideMenu属性时,将调用SideMenu()并将其分配给该属性.该实例会永远存在,即使您不使用它也是如此.

So the first time the sideMenu property is accessed, SideMenu() is called and it is assigned to the property. The instance stays around forever, even when you're not using it.

现在让我们看看另一种方法.

Now let's see another approach.

var _sideMenu: SideMenu?
var sideMenu: SideMenu! {
    get {
        if let sm = _sideMenu {
            return sm
        } else {
            let sm = SideMenu()
            _sideMenu = sm
            return sm
        }
    }
    set(newValue) {
        _sideMenu = newValue
    }
}

(请注意,这仅适用于类,不适用于结构.)

(Note this only works for classes, not structs.)

好的,这是怎么做的?好吧,它的行为与lazy var非常相似,但是让我们将其重新设置为nil.因此,如果您尝试访问sideMenu,则可以确保获得一个实例(存储在_sideMenu中的实例或新实例).这是一种类似的模式,因为它懒惰地加载SideMenu(),但是该实例可以创建许多SideMenu()实例,而上一示例只能创建一次.

OK so what does this do? Well it behaves very similarly to the lazy var, but it let's you re-set it to nil. So if you try to access sideMenu, you are guaranteed to get an instance (either the one that was stored in _sideMenu or a new one). This is a similar pattern in that it lazily loads SideMenu() but this one can create many SideMenu() instances, where the previous example can only create one once.

现在,大多数视图控制器都足够小,您可能应该只使用较早版本的lazy.

Now, most view controllers are small enough that you should probably just use lazy from earlier.

因此,针对同一问题的两种不同方法.两者都有优点和缺点,并且在不同情况下效果会更好或更差.

So two different approaches to the same problem. Both have benefits and drawbacks, and work better or worse in different situations.

这篇关于Swift Lazy和Optional属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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