Jump to

While doing programming in any language, we need different types of variables to store the information, and data types specify the type of data that can be stored inside a variable.

So, based on the type of data of a variable, the operating system actually allocates the memory and decides what can be stored in reserved memory or not.

Swift offers a collection of built-in data types. Each and everything is associated with data type. 

In general terms, a data type is the type of data a variable or constant can store in it. These are the data types that we found in other programming languages as well.  

Swift also offers a variety of other data types such as Array, Set, etc. (a.k.a Collection Types). Let’s discuss the basic data types in detail.

1. Bool:

It can store only two values i.e. either True or False.  

Default value : false 

import Foundation  

var n = 10  

var flag = false  

if(n == 20) {  

 flag = true  

}  

print(flag)  

Output : true  

2. Character:

It is a single character string literal.  

import Foundation  

var str = “H”  

print(str)  

Output : H 

3. String:

It is the ordered collection of characters. They are used to display text in an app. It is  wrapped in double quotes and also known as String Literal.

Default value : ” ” (Empty string) 

import Foundation  

var str = “Hello”  

print(“\(str)”)  

Output : Hello  

4. Int or UInt:

It can store Whole numbers both positive and negative including 0, No fractional  part allowed.  

Default value : 0 

Int can store both positive and negative small numbers.  

UInt can store only unsigned numbers i.e. positive or 0. 

import Foundation  

var number = 10  

print(number)  

Output : 10 

5. Float:

It can store numbers with fractional components, representing 32 bit floating point number. 

Default value : 0.0

import Foundation  

var number = 20.46474  

print(number)  

Output : 20.46474  

6. Double:

It can store numbers with fractional components as Float but with larger floating  point values, representing 64 bit floating point number. 

Default value : 0.0 

import Foundation  

var n = 100.2323737321212121  

print(n)  

Output : 100.2323737321212121 

Range of datatype variables:

TYPE  BIT WIDTH  RANGE
Int8  1 Byte  -127 to 127
UInt8  1 Byte  0 to 255
Int32  4 Bytes  -2147483648 to 2147483647
UInt32  4 Bytes  0 to 4294967295
Int64  8 Bytes  -9223372036854775808 to 9223372036854775807
UInt64  8 Bytes  0 to 18446744073709551615
Float  4 Bytes  1.2E-38 to 3.4E+38 (~6 digits)
Double  8 Bytes  2.3E-308 to 1.7E+308 (~15 digits)

So, these are the built-in and most known data types.  Apart from using these data types, we have Tuples, Optionals, etc. We’ll discuss those in detail in the next few articles.

We hope you got the idea of the different data types in Swift. If you are looking for challenging opportunities at some of the fastest-growing global companies, join Talent500. 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

You may also like

Kubernetes

What is CI/CD: CI/CD Pipelines

DevOps teams focus on reliable releases, faster delivery cycles, and reduced errors. Decision makers, on the other hand, seek a process that coordinates development tasks, ensures code quality, and limits

Kubernetes

Top 8 Continuous Deployment Software

You’re here because you fit into one of three categories: Whatever your situation, choose wisely because your continuous deployment tool will determine your development speed, code quality and team productivity.

Kubernetes

What Are Version Control Systems

Version control systems record changes made to files. They store revisions in a structured way. They allow teams to see what changed, when it changed, and who changed it. They

Scroll to Top