O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Explicit types

Type inference is a very nice feature in Swift and is one that you will probably get used to very quickly. However, there are times when we would like to explicitly define a variable's type. For example, in the preceding example, the variable x is inferred to be Double, but what if we wanted the variable type to be Float? We can explicitly define a variable type like this:

  var x: Float = 3.14 

Notice the Float declaration (the colon and the word Float) after the variable identifier. This tells the compiler to define this variable to be that of the Float type and gives it an initial value of 3.14. When we define a variable in this manner, we need to make sure that the initial value is of the same type that we are defining the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required