Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple variable in swift

Tags:

swift

I know its too basic but I just don't get this. You want to set some variable to your class (UIView) , and only later set it with some value, so this would give me an error on the init method :

var cellSize:CGFloat

    override init (frame : CGRect)
    {

        super.init(frame : frame)
        cellSize=self.frame.size.width

Then i am trying this :

  var cellSize:CGFloat

        cellSize=1.0
        super.init(frame : frame)


        cellSize=self.frame.size.width

Which compile, and seems very wrong . Also this one is working too :

  var cellSize:CGFloat=1.0
    override init (frame : CGRect)
    {

        super.init(frame : frame)

        cellSize=self.frame.size.width

Which works but also seems very wrong because I'v already told him its a CGFloat

Why if I tell the complier that this is a CGFloat , he won't let me continue without setting a value to it ?

like image 639
Curnelious Avatar asked May 12 '26 23:05

Curnelious


1 Answers

All the class variable or the instance variable should be initialised before a class is instantiated. The reason for it is any instance of class would need the class variable and instance variable and without a value that leads to chaos. There for you need to initialise it.

Swift on the other hand provide an exception to above said concept by providing optionals which are nothing but optional class or instance variable, however you have to insure that these variables will be initialised later in class before using it. The optional variables are represented by an exclamation mark as suffix i.e var cellSize:CGFloat!

coming back to question the first code -

var cellSize:CGFloat

override init (frame : CGRect)
{

    super.init(frame : frame)
    cellSize=self.frame.size.width

You haven't provided any value or in other words have not initialised it hence it doesn't compile ( cellSize need value before super.init(frame:frame) is called.

On the second block -

var cellSize:CGFloat

    cellSize=1.0
    super.init(frame : frame)


    cellSize=self.frame.size.width

It compiles for same reason it gets a value before calling super.init(frame:frame). Why does it seems wrong? it simply gives cell size an initial value of 1.0 and later override the value with self.frame.size.width by doing cellSize=self.frame.size.width

Third does work for the same reason because you are giving it an initial value at declaration itself.

The right way is to create cellSize as optional for the purpose i assume from your using this -

var cellSize:CGFloat!
// By doing this you tell the compiler that Hey! I am not initialising this now and I guarantee to provide a value before I use this and if i fail to do so please spit an error.
override init (frame : CGRect)
{

super.init(frame : frame)

cellSize=self.frame.size.width
}

Hope this is clear.

BR

like image 120
Jeet Avatar answered May 16 '26 06:05

Jeet



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!