Reputation: 1531
As a swift newbie I wonder what the difference between:
var img : UIImageView
var img = UIImageView()
I see them used a lot in the same situation
Upvotes: 3
Views: 134
Reputation: 1001
The first one is a variable declaration and there you set the type of the variable, not a value.
The second line sets a new instance of the class.
Since swift is strong typed everything must have a type, so if you want to store something in a var you need to set the type of the var first, therefore the first line, on the second one is classic instantiation of the variables.
Upvotes: 1
Reputation: 4583
the first example var img: UIImageView
creates a variable the WILL be of type UIImageView, once something is assigned to it. This line itself does not initialize or create a new instance and its not assigned to anything.
the second example is actually creating a new instance of UIImageView assigned to the variable that is called img. It infers from the instance create of UIImageView that it will a variable of type UIImageView so no need to type it, its redundant
Upvotes: 8