Swift Data Types

In this tutorial you will learn about the Swift Data Types and its application with practical example.

Swift Data Types

Variables are used to represent reserved memory locations that is used to store values, when we create a variable we are a suppose to allocate some memory space for that variable. Swift is a statically typed programming language. This means that variables always have a specific type and that type cannot change. Every variable have data type associated to it, data type for a variable defines –

Table Of Contents
  • The amount of memory space allocated for variables.
  • A data type specifies the possible values for variables.
  • The operations that can be performed on variables.

Swift has following built-in data types –

  • Numbers
  • Boolean
  • Characters
  • String
  • Optional

Swift Numbers:- The Number data type is used to hold the numeric values. Swift supports following numerical data types –

Integer:- Integers are used to store whole numbers. Swift supports several integer types, varying internal sizes for storing signed and unsigned integers.

Signed Integers
Type Size Description Range
int8 8 bits 8 bit Signed Integer (two’s complement) -128 to 127
int16 16 bits 16 bit Signed Integer (two’s complement) -215 to 215 -1
int32 32 bits 32 bit Signed Integer (two’s complement) -231 to 231 -1
int64 64 bits 64 bit Signed Integer (two’s complement). They can also be represented in octal and hexadecimal -263 to 263 -1
Unsigned Integers
Type Size Description Range
uint8 8 bits 8 bit Unsigned Integer 0 to 127
uint16 16 bits 16 bit Unsigned Integer 0 to 216 -1
uint32 32 bits 32 bit Unsigned Integer 0 to 232 -1
uint64 64 bits 64 bit Unsigned Integer 0 to 264 -1

 

Swift Float:- A Float type is used to store numbers that contain a decimal component (real numbers). In Swift, float is used to represent a 32-bit floating-point number and numbers with smaller decimal points. An uninitialized float has default value of 0.0.

Example:-

Swift Double:- In Swift, double is used to represent a 64-bit floating-point number and numbers with larger decimal points. An uninitialized double has default value of 0.0.
Example:-

Swift Bool:- The Boolean data type is used to represent the truth values, which can be either True or False. Boolean are commonly used in decision making statements.

Example:-

Output:-

Swift Characters:- The character data type is used to hold the single literal. Character are declared using double quotes. It can also include emoji or languages character other than English using escape sequence \u{n} (unicode code point ,n is in hexadecimal)

Example:-

Swift String:- A string variable is used to hold series or sequence of characters – letters, numbers, and special characters. Strings are immutable. Strings can either be declared using double quotes “Hello World”.

Example:-

Output:-

In this tutorial we have learn about the Swift Data Types and its application with practical example. I hope you will like this tutorial.