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

I notice when I write in Swift, if I declare a label in ViewControllereither var label: UILabel!, or var label = UILabel(), and then initialize them in a method viewDidLoad, they both seem functional, work the same way. So what is the difference between the two?

In the corresponding note, when I announce var label: UILabel!, I have to write label = UILabel(frame:CGRectMake(...)), but if I use var label = UILabel(), I need to write label.frame = CGRectMake(...). Is this because it var label = UILabel()declares and instantiates an object of the class, whereas it var label: UILabel!simply declares a type variable? I am confused by what is really going on here.

Thanks in advance!

+1
source share
2
var label: UILabel!

, UILabel, (), , , - , IBOutlet, , .

var label = UILabel()

, , . , UILabel, , , viewDidLoad()

, , , , .

UILabel.

, UILabel CGRect, . , - let label = UILabel(), , , , , , .

- , , - :

 var label : UILabel?

, , , , , "" UILabel viewDidLoad. , - "?" , :

label?.text = "fred"
+2

. UILabel() UILabel no-arg label. .

, label , UILabel , label Optional .! , , , , !; , , , nil -, .

, :

var label: UILabel

, , , (, ) super.init(), , , , - , Swift.

+2
source

Source: https://habr.com/ru/post/1660034/


All Articles