# Swift Data Types

Keywords : Data types in swift programming language with examples, Swift built in data types (integer / float / double / bool / character) with examples

Here we will learn different type of swift data types (int, float, double, bool, string, character) with examples.

## Swift Data Types

Generally in any programming language we use data types to store and manipulate the data in variables based on the type of data like integers, strings, characters, bool, etc.

Whenever we create a variable in swift programming language, automatically the required space is reserved in memory for that variable and the space allocation in memory is varied based on the type of variable like integer or float or double, etc.

## Swift Basic Data Types

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

Data TypeDescriptionExample
Integer Int or UInt stand for Integer and used for numbers like 1, 2, 3, 4, 5. We can use Int32, Int64 to define 32 or 64-bit signed integer, whereas UInt32 or UInt64 to define 32 or 64-bit unsigned integer variables. For example, 345566 and -345566.

100

Float Float stand for floating value and it is used to represent numbers with fraction value like 245.344 and etc. It is used to hold the numbers with larger or smaller decimal points like 3.14, and -455.3344.

3.14

-455.3344

Double Double stands for Double means if we want to allocate the large number with fraction value then we use Double as a float data-type. We can use it as a 64-bit floating-point number and Double is more precise than Float. For example, 345.344544, and -4554.3455543.

345.344544

-4554.3455543

Bool Bool represent only two values either TRUE or FALSE. We can use Bool to check whether certain condition met or not.

let i = 10

if i> 20 {

}

String String is a combination of Characters. We can define string data type by adding double quotes to our text like “Hello World”.

""

"welcome to tutlane"

Character It represent a single alphabet character like “H”, “e”, “L”, “L”, “o”. “H”
Optional It represent a variable can hold value or no value.

## Swift Data Type Ranges

Following table shows the details like how much memory space occupied by a variable based on the data type and minimum & maximum values range for each data type.

Data TypeMemory AllocationMin & Max Range
Int8 1byte -127 to 127
UInt8 1byte 0 to 255
Int32 4bytes -2147483648 to 2147483647
UInt32 4bytes 0 to 4294967295
Int64 8bytes -9223372036854775808 to 9223372036854775807
UInt64 8bytes 0 to 18446744073709551615
Float 4bytes 1.2E-38 to 3.4E+38 (~6 digits)
Double 8bytes 2.3E-308 to 1.7E+308 (~15 digits)

## Type Aliases

In swift by using typealiases keyword we can create new name for existing type. The typealiases are useful whenever we want to create meaningful name for our existing types.

Following is the syntax of creating alternative name to existing type using the keyword typealias.

typealias <Name> = <type>

Following is the simple example of using typealias to create alternative name for “String” type.

typealias PersonName = String

var Name : PersonName =  "Tutlane"

print(Name)

If you observe above example we created alternative name “PersonName” to “String” data type.

When we compile the above the code, compiler gives the following output

Tutlane

## Type Safety

Swift is a type-safe programming language that means once we define variable with one data type then it’s not possible to assign another data type value to it.

Suppose if we define variable with String data type and if we try to pass an integer value then the compiler will throw error like as shown following because swift is a type-safe language and it perform type checks while compiling our code and flags any mismatched types as errors.

## Type Inference

In swift Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

Following is the simple example to deduce the variable value, when the compiler found that it adds the floating values to integer.

var varA = 53453

print(varA)

var varB = 53453.453453

print(varB)

var varC = 53 + 0.45353

print(varC)

When we run above program we will get output like as shown below.

53453

53453.453453

53.45353

NEXT

Swift Variables