Data types or types in general, is a classification of data, which tells what type of data is being stored, by a variable or constant.

For example,

var length:Int = 5
let aString: String = "Hello, World!"

The variable “length” stores an integer value, while “aString” stores a string.

In Swift language, a variable or constant can hold many types of data, they are listed below.

Integer

As the name suggest, an integer data type stores integer values, which is the whole number with no fractions. The keyword “Int” is used, to declare an integer type.

For examples

var intA: Int    // tells swift that the variable would store integer values
intA = 5
var intB = 10

When the value is provided at the declaration time, such as, in the case of “intB”. It is optional to specify the data type. Swift automatically matches the type of the value provided with the data type and defines a type of the variable. ( Here, “10” is provided, Swift declares the variable “intB” as an integer type. )

The type “Int” uses 32 or 64 bits form to store values, depends on the devices that are used to run a swift programme. For example, normally an apple computer works on a 64 bits processor, so an integer variable uses 64 bits to store a value. On the other hand, iPhone 4 and 5 use 32 bits processor, so the same integer variable would use 32 bits.

On the other hand, it always doesn’t have to be 32 or 64 bits. Swift is flexible enough to let programmers declare 8 or 16 bits integers as well.

Data Type Bits Minimum Value Maximum Value
Int8 8 -128 127
Int16 16 -32,768 32,767
Int32 32 -2,147,483,648 2,147,483,647
Int64 64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Swift also allows programmers to define signed or unsigned behaviour of integer types. The table above shows signed integers, which have negative minimum values. Moreover, unsigned integers have “0” as a minimum value. The unsigned integers can be declared by placing ‘U’ at the beginning of the type name.

Data Type Bits Minimum Value Maximum Value
UInt8 8 0 255
UInt16 16 0 65,535
UInt32 32 0 4,294,967,295
UInt64 64 0 18,446,744,073,709,551,615

Although, “Int8” and “UInt8” is convenient, but if the specific number of bits are not required then it is recommended to use “Int” to declare a signed integer and “UInt” to declare an unsigned integer, to simplify the programming!!

Such as,

let aValue: Int = -120
let length: UInt = 10

Floating Point Numbers

The floating point numbers are just like an integer except it has fraction values. There are 2 types of float numerals used in Swift programing, Float and Double!

Float:

A float numeral is defined using the keyword “Float”, and it is a 32 bits value.

Double:

A double numeral is defined using the keyword “Double”, and it uses 64 bits.

var aFloat: Float = 15.234
var aDouble: Double = 1333.2343

Other Numeric Literals

Swift is not limited to an integer and floating point literals. Other literals such as binary, octal and hexadecimal can also be defined by placing ‘0b’, ‘0o’ and ‘ox’ at the beginning of the numeral value.

var aBinary = 0b1001    // Output => 9
var aOctal = 0o17       // Output => 15
var aHexa = 0x8D2       // Output => 2258

Boolean

The boolean type is logical, it can only be “true” or “false”. The boolean variable can be declared using the keyword “Bool”.

For example,

let varA = true
var varB: Bool = false

The following operators can be used in Swift to produce boolean result.

Less than < 10 < 11 true
Greater than > 8 > 15 false
Equal to == 10 == 10 true
Less than or equal <= 12 <= 12 true
Greater than or equal >= 15 >= 30 false

String

The simplest way to create a string in Swift, is to encode the string of text with double quotes ( i.e. “…” ). Strings in Swift are very powerful and will be discussed in details much later.

var stringA = "Hello, World!"

Swift is type-safe language, that’s why the following example will raise an error in the code.

var length = 5
var stringA = "The length is: " + length // will throw an error

The reason is that, auto-conversions of the types are not allowed. In this example, the conversion of the variable length which is an Integer type can’t be done into the string type.
To overcome this issue, there are two ways,

var length = 5
// Option 1
var stringA = "The length is: " + String( length )
// Option 2
var stringB = "The length is: \( length )"

This will convert any variable type to the string type, then encode it with the rest of the string.

To print strings, the “print( )” function is used. For example,

print( "Hello, world!" )   
print( stringA )

Another useful method to create a string is “three double quotes”, to create a string made of multiple lines with indentation.

let quotation = """
Hello! How are you?
I am fine. thank you.
"""

Summary

In this example, we learned basic data types in Swift that can be used to store a value. We learned signed and unsigned integers, integers use 8, 16, 32, and 64-bit forms. We learned 2 floating point data types Float and Double. We also covered boolean types and the basic string types. This will be very helpful to write basic programs! In the next tutorial, we will be looking at arrays of the Swift Programming.