1

I am pretty new to swift . I found the following document from Apple.

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition. These actions are described in the following sections.

but the below piece of code noOfTyres is not initialised and the compiler doesn't complain ,please explain this .

class Vehicle
{
    var noOfTyres: Int!
    var engineCapacity: Int

    init()
    {
        engineCapacity = 10
    }

}
Kubba
  • 3,390
  • 19
  • 34
  • 2
    `noOfTyres` is _implicitly unwrapped optional_ and it's just have a default value of nil. If you would try something like `vehicle().noOfTyres + 5` you would crash your application because `noOfTyres` is not set – Maxim Kosov May 04 '16 at 13:57

3 Answers3

1

You can declare a stored property without assigning it an initial value if you make this property an optional. The property then either has a value or it is nil. An optional property is either declared with

var myOptional : Int?

or

var myOptional : Int!

And therefore noOfTyres is not initialized, while it is an optional and currently it is set to nil.

For more information, please, read the Apple documentation.

Additional information. The different types of optional declaration (! and ?) are explained in this post.

Community
  • 1
  • 1
MacUserT
  • 1,760
  • 2
  • 18
  • 30
  • I went through the post you mentioned ,as well as the documentation ,where I am confused at is "The String! is an implicitly unwrapped optional where you indicate that it will always have a value - after it is initialised" But in the piece of code which I posted in the question , it can be uninitialised as well,i may get run time error though ,if not assigned a value .But for other stored properties ,if not initialised ,the compiler will give error .How is it differing – rajesh sukumaran May 04 '16 at 14:32
  • This is true. I think it is not a good coding practice to declare an optional with the implicit unwrapped !, since you expect during run time that there will be a value and the property is not nil. If the property is nil, your app will crash. Declaring an optional property with ! will make your code less save, which is the objective of swift, write safe, fast and expressive code. I hope it answered your question though. – MacUserT May 04 '16 at 14:38
1

In your case, the compiler won't complain, until you try to use that value. Because you have the value un-wrapped (!) it assumes that it will never be nil and trying to access the value, will crash.

In this case I would add a default value to the property noOfTyres.

var noOfTyres: Int = 2

Or, you can add the value in the constructor to make sure that everytime an object gets created, the value must be set.

class Vehicle
{
    var noOfTyres : Int!
    var engineCapacity :Int

    init(noOfTyres: Int)
    {
        self.noOfTyres = noOfTyres
        engineCapacity=10;
    }

}

Remember, if it's not optional, you are saying, the property will never be nil.

Another thing, by convention class names must be capitalized.

dmlebron
  • 861
  • 6
  • 16
0

I am not sure ,if this can be the answer

The documentation says ,

Implicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter.

Hence its expected by the compiler as initialised ,definitely containing a value.But If we access it without a value during run time it will crash

If using an optional as a stored property ,it will have atlas a nil value & for other stored properties ,need an initialisation.