Swift Variables Initialization

谁说胖子不能爱 提交于 2021-02-06 11:37:39

问题


I have a question about variables initialization in swift.

I have two ways to initialize a variable (as "property" of a class in Objective-C).

Which of them is the most correct?

class Class {

  var label: UILabel!

  init() { ... label = UILabel() ... }

}

or

class Class {

  var label = UILabel()

  init() { … }

}

回答1:


Actually you have 5 ways to initialize properties.

There is no correct way, the way depends on the needs.
Basically declare objects like UILabel always – if possible – as constant (let).

The 5 ways are:

  • Initialization in the declaration line

    let label = UILabel(frame:...
    
  • Initialization in the init method, you don't have to declare the property as implicit unwrapped optional.

    let label: UILabel
    init() { ... label = UILabel(frame:...) ... }
    

The first two ways are practically identical.

  • Initialization in a method like viewDidLoad, in this case you have to declare the property as (implicit unwrapped) optional and also as var

    var label: UILabel!
    
    on viewDidLoad()
     ...
     label = UILabel(frame:...)
    }
    
  • Initialization using a closure to assign a default (computed) value. The closure is called once when the class is initialized and it is not possible to use other properties of the class in the closure.

    let label: UILabel = {
       let lbl = UILabel(frame:...)
       lbl.text = "Foo"
       return lbl
    }()
    
  • Lazy initialization using a closure. The closure is called (once) when the property is accessed the first time and you can use other properties of the class.
    The property must be declared as var

    let labelText = "Bar"
    
    lazy var label: UILabel = {
       let lbl = UILabel(frame:...)
       lbl.text = "Foo" + self.labelText
       return lbl
    }()
    



回答2:


Both ways are correct, but sometimes you should use the initialization in init() method. For example, here the target for barButton will be not set, cause the self does not exist yet.

class Foo {
    var barButton = UIBarButtonItem(title: "add", style: .Plain, target: self, action: #selector(self.someMethod))

    init(){
        //init here
    }
}

The correct way for this case is :

class Foo {
    var barButton : UIBarButton? 

    init(){
        barButton = UIBarButtonItem(title: "add", style: .Plain, target: self, action: #selector(self.someMethod))
    }
}

To sum up, both ways are correct, but you have to consider when to use each one. More information about it on Apple documentation




回答3:


 var label: UILabel! 

The above statements means, the variable label may be nil

var label = UILabel()

But here it will hold a label value. Because the memory is allocated for that variable. So it will not be nil.

Please find more details here

What's the difference between var label: UILabel! and var label = UILabel( )?



来源:https://stackoverflow.com/questions/40460118/swift-variables-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!