Quick iOS distinction between: and ()

As a quick newbie, I am wondering what the difference is between:

var img : UIImageView
var img = UIImageView()

      

I see they used a lot in the same situation

+3


source to share


2 answers


the first example var img: UIImageView

creates a WILL variable of type UIImageView as soon as something is assigned to it. This line itself does not initialize or create a new instance and does not bind to anything.



the second example actually creates a new instance of UIImageView assigned to a variable called img. It deduces from the create UIImageView instance that it will be a variable of type UIImageView so no need to inject it, its redundant

+8


source


The first is the declaration of a variable and there you specify the type of the variable, not the value.

The second line sets up a new instance of the class.



Since swift is strong typed everything has to be of type, so if you want to store something in var you need to set the var type first, so the first line to the second one is a classic instance of variables.

+1


source







All Articles