Lesson 3 – Integers, Decimal Numbers, Strings and Combining Values of Different Types

Values in Swift have different types depending on what they represent

Swift has data types for working with numbers and strings. You will learn about all of them in this lesson.  

The first data type is Int. It stores whole numbers such as game scores.

The colon after the variable’s name declares its type. 

Swift knows the type of constants and variables based on the values they store. This process is called type inference.

Type inference determines that score is an Int variable. The minus sign declares a negative whole number like it would in Maths.

The following data type is Double. It stores decimal numbers such as temperatures.

The decimal point declares a decimal number like it would in Maths. 

Type inference works with Double constants and variables as well.

Type inference determines that temperature is a Double variable. The minus sign declares a negative decimal number like it would in Maths. 

The last data type is String. It stores strings such as temperature types.

The temperature symbol is stored between double-quotes.

Type inference works with String constants and variables too.

Type inference determines that degrees is a String constant.

Create custom strings with dynamic data using string interpolation and string concatenation

String interpolation creates strings from other data types.

The backslash converts the game’s score from an Int variable to a String constant. The interpolated Int value is placed between round brackets. 

There may be as many interpolations in the custom string as needed.

Today’s temperature is interpolated from the temperature’s Double value and String symbol.

String concatenation uses the addition operator with strings.

The strings representing my first and last name are concatenated with a space between them to get my full name. 

String interpolation works well with string concatenation.

The above code:

  1. Creates a string from the version constant.
  2. Concatenates the strings representing the programming language’s name and latest version with a space between them to get the latest full version of Swift.

Strong data types help you avoid bugs in your programs

Suppose the score variable stores Double values since game scores may be decimal numbers.

The compiler flags a type mismatch error for the above code.

error

This happens because  score is an Int variable, so it can only store whole numbers. The error disappears for Double constants.

Int values can apparently be stored inside Double variables:

The temperature’s value is still a Double one. Check this out by printing the temperature variable to the console.

As you would have expected, the temperature’s value is 20.0 instead of 20. 

It may sound convenient to store different types of values in the same constant or variable. This creates bugs, so Swift doesn’t let you do that.

It is sometimes useful to convert data from one data type to another

Suppose you want to compute the average level’s score for your favorite game:

The above code:

  1. Subtracts the lowest level’s score from the highest level’s score. 
  2. Divides the difference by the number of levels. 

The average level’s score is 2 instead of 2.45. This happens because the divided values are whole numbers.

Type casting converts data types between them.

Type casting converts Int constants to Double ones. The converted Int values are stored between round brackets. 

Int values are converted to Double ones with only zeros as decimals. The divided values are decimal numbers, so the result is the right one.

Type casting also converts numbers to strings. 

Type casting converts the current level’s score to a string.

String concatenation works well with type casting.

The above code:

  1. Casts the version constant to a string.
  2. Concatenates the strings representing the programming language’s name and latest version with a space between them to get the latest full version of Swift.

Register to get more exclusive free content

You can follow the entire course for free, with no registration required. Creating a free account gives you these extra benefits:

  • Enroll in the course and track your progress;
  • Download a free Swift cheat sheet;
  • Get access to other exclusive guides and courses available only to registered users.

Click here to register

Already have an account? Login here

 

 

 

Back to: Learn Swift > Language basics