The Talent500 Blog
DataTypes in Swift 1

DataTypes in Swift

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. 

 

1+
Saumya Saxena

Saumya Saxena

iOS enthusiast with 3+ years of experience in the IT industry.
As an iOS developer, I have a strong passion for developing applications. Apart from being into Mobile development, I have been constantly working towards Technical Writing. Working for my passion provides up thrust to me and aids me in pursuing the same as my future career. I believe in working hard.

Add comment