简体   繁体   English

我们是否必须在viewDidLoad中或UIViewController中的viewDidLoad外部初始化属性?

[英]Do we have to initialize properties in viewDidLoad or outside of viewDidLoad in UIViewController?

I read an article says property needs to be nil when UIViewController class created, and then storyboard or you add it to value when viewDidLoad invoked. 我读了一篇文章说当创建UIViewController类时, 属性必须为nil ,然后创建故事板,或者在调用viewDidLoad时将其添加到值。

It says properties can not be initialized when object is constructed also. 它说在构造对象时也不能初始化属性。

According to article it would be like below Note that it is custom button not from storyboard. 根据文章,如下所示。请注意,它是来自故事板的自定义按钮。

class BetterViewController: UIViewController {
    var myButton: UIButton!

override func viewDidLoad() {
        super.viewDidLoad()
        myButton = UIButton() // I set value to myButton.
       let anotherVariable = myButton.method // whatever method 
}

Above, myButton was type of nil first and then I set a value to it when viewDidLoad. 上面,myButton首先是nil类型,然后在viewDidLoad时为其设置一个值。 Everything is okay so far. 到目前为止一切都还好。 But I see so many codes like below 但是我看到很多像下面的代码

class ToDoListVewController: UITableViewController {

    let defaults = UserDefaults.standard  
    var itemArray = [Item]() // 

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        if let items = defaults.array(forKey: "ToDoListArray") as? [String]{
            itemArray = items
        }
    }

Above example, itemArray is not nil. 上面的示例中,itemArray不为nil。 But it needs to be nil when UITableViewController created. 但是创建UITableViewController时,它必须为nil。 So I have to declare itemArray inside viewDidLoad or it is ok? 所以我必须在viewDidLoad里面声明itemArray还是可以的? I am confused. 我很困惑。

Without any more context, the article is wrong. 没有更多的上下文,该文章是错误的。 You obviously can have properties initialised inline, just like the examples you showed. 显然,您可以使属性内联初始化,就像显示的示例一样。 If you want me to explain why the article says that, you should include a link to the article. 如果您想让我解释文章为什么这么说,则应包括该文章的链接。

Generally, we would like set the property to nil and initialise it in viewDidLoad when the initialisation of the property requires the use of self . 通常,当属性的初始化需要使用self时,我们希望将属性设置为nil并在viewDidLoad 对其进行初始化 Gesture recognisers are a good example of this. 手势识别器就是一个很好的例子。 We cannot do this: 我们不能这样做:

let myGestureRecognizer = UITapGestureRecognizer(target: self, selector: ...)

because self is used. 因为使用了self By the time the above executes, self may not have been constructed properly, so the compiler prohibits this kind of thing. 到上述执行时, self可能尚未正确构造,因此编译器禁止这种情况。 Therefore, we can only set it tot nil: 因此,我们只能将其设置为nil:

var myGestureRecognizer: UITapGestureRecognizer!

and initialise it in viewDidLoad : 并在viewDidLoad初始化它:

myGestureRecognizer = UITapGestureRecognizer(target: self, selector: ...)

On the other hand, if the initialisation of the property does not require self to be constructed, you can initialise it inline. 另一方面,如果属性的初始化不需要构造self ,则可以内联初始化。 For example, if you want to keep track of how many times a user has tapped on a button: 例如,如果要跟踪用户轻击按钮的次数:

// Without doubt, this will be 0 at the start
var buttonPressedCount = 0

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

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