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.

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

Character

It is a single character string literal.  

import Foundation  

var str = "H"  

print(str)  

Output : H

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

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

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

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

Illustration of a distributed edge computing network with local devices, sensors, and cloud connectivity

What is Edge computing? Everything you need to know

In today’s data-driven world, devices such as sensors, autonomous machines, and advanced healthcare equipment are constantly generating massive volumes of data.  Traditionally, this data is transmitted to a centralized server

Illustration of end-to-end (E2E) testing workflow showing connected frontend, backend, and database systems for software quality assurance

End-to-End Testing: A Complete Guide for Modern Software Teams

In today’s fast-paced world of software development, delivering reliable, high-quality applications is no longer optional but it’s essential. As products grow more complex, involving numerous frontend interfaces, backend services, databases,

Categories
Interested in working with Mobile ?

These roles are hiring now.

Loading jobs...
Scroll to Top