Swift Tutorial Swift References

Swift - Data Types



One of the most important parts of learning any programming language is to understand what are the available data types, and how data is stored, accessed and manipulated in that language. Based on the data type of a variable, the operating system allocates memory to the variable and decides what can be stored in the reserved memory.

Swift Basic Data Types

The following are the basic and most frequently used data types available in the swift programming language.

  • Int − An integer type, which has the same size as the current platform's native word size:
    • On a 32-bit platform, Int is the same size as Int32.
    • On a 64-bit platform, Int is the same size as Int64.
    Int8, Int16, Int32, Int64 can be used to represent 8 Bit, 16 Bit, 32 Bit, and 64 Bit forms of signed integer. For example, -10, 10, and 50.
  • UInt − An unsigned integer type, which has the same size as the current platform's native word size:
    • On a 32-bit platform, UInt is the same size as UInt32.
    • On a 64-bit platform, UInt is the same size as UInt64.
    UInt8, UInt16, UInt32, UInt64 can be used to represent 8 Bit, 16 Bit, 32 Bit and 64 Bit forms of unsigned integer. For example, 10, and 50.
  • Float − Represents a 32-bit floating-point number and numbers with smaller decimal points. For example, 3.14159, 0.1, and -273.15.
  • Double − Represents a 64-bit floating-point number and used when floating-point values are very large. For example, 3.14159, 0.1, and -273.15.
  • Bool − Represents a Boolean value which is either true or false.
  • String − A series of characters. For example, "Hello, World".
  • Character − A single-character string literal. For example, "A"
  • Optional − Represents a variable that can hold either a value or no value.
  • Tuples − Used to group multiple values in single compound value. The values within a tuple can be of any type and don't have to be of the same type as each other.

Bound Values

The table below describes the variable type, memory size, and maximum and minimum value which can be stored in the variable.

TypeMemory SizeRange
Int81byte-127 to 127
UInt81byte0 to 255
Int324bytes-2,147,483,648 to 2,147,483,647
UInt324bytes0 to 4,294,967,295
Int648bytes-(263) to (263)-1
UInt648bytes0 to 18,446,744,073,709,551,615
Float4bytes1.2E-38 to 3.4E+38 (~6 digits)
Double8bytes2.3E-308 to 1.7E+308 (~15 digits)

Type Aliases

Type aliases define an alternative name for an existing type. To define a type aliases, the typealias keyword is used:

typealias newname = type

Example:

In the example example, an alternative name MyInt is given to Int type.

typealias MyInt = Int

var i:Int = 100
var j:MyInt = 200

print("i = \(i)")
print("j = \(j)")
print("i + j = \(i + j)")

The output of the above code will be:

i = 100
j = 200
i + j = 300

Type Safety

Swift is a type-safe language. A type safe language encourages the user to be clear about the types of values the code can work with. If the part of the code requires a String, the user can't pass it an Int by mistake.

Because Swift is type safe, it performs type checks when compiling the code and flags any mismatched types as errors. This enables the user to catch and fix errors as early as possible in the development process.

Example:

In the example below, a variable i is initialized with a String value. Later on it is used to store an Int value. Due to type mismatch, type error is raised.

var i = "Hello World"
i = 100

print(i)

The output of the above code will be:

Main.swift:2:5: error: cannot assign value of type 'Int' to type 'String'
i = 100

Type Inference

Swift uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles the code, simply by examining the values provided to it.

Example:

Consider the example below:

//x is inferred to be of type Int
var x = 42
print(x)

//y is inferred to be of type Double
var y = 3.14159
print(y)

//z is inferred to be of type Double
var z = 3 + 0.14159
print(z)

The output of the above code will be:

42
3.14159
3.14159