Skip to content
Related Articles

Related Articles

Swift – Data Types

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 04 May, 2022

Data types are the most important part of any programming language. Just like other programming languages, in Swift also the variables are used to store data and what type of data is going to store in the variable is decided by their data types. As we know that variables are nothing but a reserved memory to store data so according to these data types operating system allocates memory and stores only the specified type data in the allocated memory, for example, the integer type variable can only store integer value it can not store string value. Swift support six data types Int, String, Float, Double, Bool, and Character. Let’s discuss these data types in detail with examples.

1. Integer: In Swift, the integer is a data type that is used to represent a whole number with no fraction, for example, “2”, “56”, “3”, etc. Integer can be of 8, 16, 32, and 64 signed or unsigned bits. A variable of integer data type can be defined using the Int keyword. Int can be represented as a signed integer and UInt represents an unsigned integer.

Syntax:

var num : Int = 199

Integer Bound Value: Just like other languages in Swift also we can find the minimum and maximum value of each integer type. Their minimum and the maximum value represent the integer bound.

Type SizeRange
Int88 bit-128 to 127
Int1616 bit-215 to 215-1
Int3232bit-231 to 231-1
Int6464bit-263 to 263-1
UInt32Depend on platform0 to 232
UInt64Depend on platform0 to 264

Example:

Swift




// Swift program to demonstrate integer datatype
  
// Creating signed integer data types
var digit1: Int = 10
  
// Display the number
print("Signed integer data type is", digit1)
  
// Creating unsigned integer data type
var digit2: UInt = 23
  
// Display the number
print("Unsigned integer data type is", digit2)

Output:

Signed integer data type is 10
Unsigned integer data type is 23

2. String: In Swift, the string is used to represent a sequence of characters or textual data, for example, “GeeksforGeeks”. The variable of string data type is defined using the String keyword. 

Syntax:

var num : Int = 199

Example:

Swift




// Swift program to demonstrate string datatype
  
// Creating string data type
var inputdata1: String = "GeeksforGeeks"
  
// Display the value
print("String data type is", inputdata1)
  
// Creating string data type
var inputdata2: String
inputdata2 = "GFG"
  
// Display the value
print("String data type is", inputdata2)

Output:

String data type is GeeksforGeeks
String data type is GFG

3. Float: In Swift, the float data type is used to represent decimal numbers or we can say that float is used to represent 32-bit floating-point numbers, for example, “3.23”, “0.89753”, etc. It stores data with less precision. This data type represents up to 6 decimal places, so it is generally used when the floating-point number is less. If you want to store more decimal places then you can use a double data type. The range of float data types is 1.2E-38 to 3.4E+38. The variable of float data type is defined using the Float keyword. 

Syntax:

var inputData : Float = 3.43

Example: 

Swift




// Swift program to demonstrate float datatype
  
// Creating float data type
var inputdata1: Float = 3.0545 
  
// Display the value
print("Float data type is", inputdata1)
  
// Creating double data type
var inputdata2: Float = 0.978623
  
// Display the value
print("Float data type is", inputdata2)

Output:

Float data type is 3.0545
Float data type is 0.978623

4. Double: Double data type is used to represent a number with fractional components or we can say that double data type is a 64-bit floating-point number, for example, “2.3345656”, “0.24354656”, etc. This data type represents up to 15 demical places and it is generally used when the floating-point number is large. The range of double data types is 2.3E-308 to 1.7E+308. The variable of double data type is defined using the Double keyword. 

Syntax:

var inputData : Double = 23.45788

Example:

Swift




// Swift program to demonstrate double datatype
  
// Creating double data type
var inputdata1: Double = 23.098545 
  
// Display the value
print("Double data type is", inputdata1)
  
// Creating double data type
var inputdata2: Double = 1.9786677532
  
// Display the value
print("Double data type is", inputdata2)

Output:

Double data type is 23.098545
Double data type is 1.9786677532

5. Bool: In Swift, bool or boolean is used to represent logical entities. This data type has only two values that is true or false. Here the variable of boolean data type can be defined using the Bool keyword.

Syntax:

var inputData : Bool = true

Example:

Swift




// Swift program to demonstrate boolean datatype
  
// Creating boolean data type
var inputdata1: Bool = true 
  
// Display the value
print("Boolean data type is", inputdata1)
  
// Creating boolean data type
var inputdata2: Bool = false
  
// Display the value
print("Boolean data type is", inputdata2)

Output:

Boolean data type is true
Boolean data type is false

6. Character: In Swift, the character is used to represent a single letter or string literal. For example “G”, “E”, “K”, “S”, etc. A variable of character data type can be defined using the Character keyword.

Syntax:

var inputData : Character = “G”

Example: 

Swift




// Swift program to demonstrate character datatype
  
// Creating character data type
var inputdata1: Character = "G"
  
// Display the value
print("Character data type is", inputdata1)
  
// Creating character data type
var inputdata2: Character = "E"
  
// Display the value
print("Character data type is", inputdata2)

Output:

Character data type is G
Character data type is E

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!