Basic Data type sin Swift and Coverting Data types.

Data Type and Type Conversion in Swift

Tested on: Xcode 12.2, Swift 5.3

What is Data Types?

In computer science, Data Type is an attribute of data that inform the compiler how to interpret or use the data. For example, when you use a numeric data type, you inform the compiler that this is a number and so, the compiler knows how to use and process them.

In this reference, you will learn about the basic of data types and also, type conversion in which you are able to convert one data type to another type.

  1. Data Type in Swift?
  2. Type Coversion





1. Basic Data Types in Swift

Data type in Swift

The four basic data types are Integer, Float, String and Boolean.

  • Integer - Integer is numeric numbers without any decimals. The example below shows numberOfApple is 2 and 2 is a whole number and therefore, it is Integer.

    Swift Code :

    var numberOfApple = 2

  • Float - Float is numeric numbers with decimals. For example, the value below shows costOfApple is 1.20. Since the number contains a decimal place. The costOfApple is a Float and not an integer.

    Swift Code :

    let costOfApple = 1.20

  • String - String is a collection of characters. For example, my name is Swee Kwang. Thus, my name contains characters such as "S", "w", "e", "e", " ", "K", "w", "a", "n", "g". Note that a space is a character. In Swift and most programming language, characters are case-sensitivity meaning "E" is not the same as "e".

    Swift Code :

    let myName = "Swee Kwang"

  • Boolean - Boolean is true or false. For example, if you want to store the information about the setting of app such as isDarkMode. You can store the information true or false. If it is true, the app appearance is dark mode.

    Swift Code :

    let isDarkMode = true



2. Type Conversion

Swift is a strongly typed language and is very strict about the data type you assigned to the variable.

Look at the following two examples below:

Swift Code :

let costOfApple = 1.2
let numberOfApple = 12
let totalCost = numberOfApple * costOfApple // Error

Output :

error: binary operator '*' cannot be applied to operands of type 'Double' and 'Int'

There is an error because you can't mix the data types and perform an operation. If you look at their data type costOfApple has a data type of Double (Double a is numeric data type with decimal) while numberOfApple is Int. And Swift are not happy about it because it wants you to explicitly state the conversion.

Therefore, you have to change their data types to be the same so that you can perform the operations.

You can change costOfApple to Int or numberOfApple to Double. However, if you change costOfApple to Int, it will result in a loss of precision like the one below:

Swift Code :

let costOfApple = 1.2
print(Int(costOfApple))

Output :

1

That's wrong! The cost of apple is $1.2 and not $1. Thus, when converting data types, it is important to know what you want to do.

How to convert Data Type?

DataType(value) - The simplest and the easiest way is the state the data type and add the value between the brackets likes:

Swift Code :

let numberOfApple = 1

Double(numberOfApple)
String(numberOfApple)