简体   繁体   English

为什么UIViewController的'view'属性在Swift中不是可选的?

[英]Why is the 'view' property of UIViewController not optional in Swift?

I noticed that the view property of UIViewController isn't of type UIView? 我注意到UIViewControllerview属性不是UIView?类型的UIView? but UIView instead (ie not optional). 但相反UIView (即不是可选的)。 Why is that? 这是为什么? Aren't UIViewController's views supposed to be nil when not loaded yet? UIViewController的视图在未加载时应该是nil吗?

The offical documentation states the following: 官方文档说明如下:

The view stored in this property represents the root view for the view controller's view hierarchy. 存储在此属性中的视图表示视图控制器的视图层次结构的根视图。 The default value of this property is nil. 此属性的默认值为nil。 .... The UIViewController class can automatically set this property to nil during low-memory conditions and also when the view controller itself is finally released. ....在低内存条件下以及最终释放视图控制器本身时,UIViewController类可以自动将此属性设置为nil。

From my understanding of Swift optionals, it seems that view should be declared as var view: UIView? 根据我对Swift选项的理解,似乎应该将view声明为var view: UIView? since it may be nil at some point. 因为在某些时候它可能是nil Apple seems to state the opposite, can you explain my why? Apple似乎反过来说,你能解释一下我的原因吗?

What do you think? 你怎么看?

It looks like view is a lazy property (but not using the lazy modifier) - in fact if you read the inline comment (cmd+click on the view property to open the source) it says: 看起来view是一个惰性属性(但不使用lazy修饰符) - 实际上如果你读取内联注释(cmd +点击view属性来打开源代码),它会说:

The getter first invokes [self loadView] if the view hasn't been set yet. 如果尚未设置视图,则getter首先调用[self loadView]。 Subclasses must call super if they override the setter or getter. 如果子类覆盖了setter或getter,则必须调用super。

and about loadView() : 和关于loadView()

This is where subclasses should create their custom view hierarchy if they aren't using a nib. 这是子类在不使用nib时应创建自定义视图层次结构的位置。 Should never be called directly. 永远不应该直接打电话。

I presume that: 我认为:

  • the standard implementation creates an empty view if it is not instantiated otherwise 如果没有实例化,标准实现会创建一个空视图
  • it is backed by an optional variable (not publicly accessible) 它由可选变量支持(不可公开访问)
  • in case of low memory conditions, the backing variable is set to nil, releasing the view instance 在内存不足的情况下,后备变量设置为nil,释放视图实例
  • if you try to access the property when the backing variable is nil, it creates a new instance 如果在后备变量为nil时尝试访问该属性,则会创建一个新实例

so its implementation should be conceptually similar to: 所以它的实现应该在概念上类似于:

private var _view: UIView?

var view: UIView {
    if _view == nil {
        loadView()
    }
    return _view!
}

func loadView() {
    if _view == nil {
        // _view = something()
    }
}

The property is technically not nil. 该物业在技术上不是零。 It initialises the view when the property is accessed first time. 它在第一次访问属性时初始化视图。 It is called lazy loading . 它被称为延迟加载 That means, when you access the property you are guaranteed to receive a value. 这意味着,当您访问该房产时,您将获得一个价值。 I think the documentation for this property is misleading if not even incorrect. 我认为这个属性的文档有误导性甚至不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM