Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift iOS difference between : and ()

Tags:

xcode

ios

swift

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

like image 331
Kiwo Tew Avatar asked Dec 12 '25 11:12

Kiwo Tew


1 Answers

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

like image 186
bolnad Avatar answered Dec 15 '25 01:12

bolnad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!