Table of contents
1.
Introduction
2.
Data Type - Size
3.
Type Aliases in Swift
4.
Type Safety in Swift
5.
Built-in Data Types
5.1.
Integer
5.2.
Boolean
5.3.
Float
5.4.
Double
5.5.
Character
5.6.
String
6.
Range Table
7.
Frequently Asked Questions
7.1.
What is Optional in built-in data types in Swift?
7.2.
What are Tuples in built-in data types in Swift?
7.3.
Does Swift provide type inference?
8.
Conclusion
Last Updated: Mar 27, 2024

Swift - Data Types

Author Gurleen Kaur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We know that data types are primarily used while declaring the variables or constants, and variables are the reserved memory locations that store values. Also, each programming language uses different variables. So, to define the data type, we say that the type of value allowed to store in a variable is called a data type.

Let us explore more into the details of data type and its type offered by the Swift 4 programming language.

Data Type - Size

The size of the data stored in a variable or constant, is called the size of the data type.

This is calculated in bits and stores up to 2 bits .

Examples:

Data Type - 1bit
Stores- 2 1 = 2 values that is 0 or 1.
Data Type - 2bit
Stores- 2 2 = 4 values which are 00 -> a, 01 ->b, 10->c, 11->d

Type Aliases in Swift

Swift Aliases provides a feature to help users provide another name for an existing type.

The syntax used to define the type aliases is as given below.

typealias newname = type

Example:

In this example, the statements tell the compiler that Marks is another name for Int.

typealias Marks = Int

The given code works perfectly fine and provides an integer variable named Physics.

typealias Marks = Int 
var Physics: Marks = 95 
print(Physics)

Output:

95

Type Safety in Swift

Once a variable is defined of a type, you can not give it some value of another type. This means the Swift is a type-safe language.

So, the code is checked at the compile-time, and it gives an error in case it doesn’t work well.

Example-

var x = 7
x = "Coding Ninjas"
print(x)

Output-

main.swift:2:11: error: cannot assign value of type 'String' to type 'Int'
x = "Coding Ninjas"

Built-in Data Types

Swift 4 programming language provides many data types to store information in a variable or constant. These are explained as follows-

Integer

  • Swift integer data type allows the user to store and represent integer numbers with no fractional part.
  • The “Int” keyword is used to declare the variables.
  • The default value for it is 0.
  • The platform type decides the size of the data type. It can be either 32 bits or 64 bits.
  • There are variations of Int to define unsigned integer type. This is done using UInt32, UInt64, etc., where 32 and 64 are the bits for the platform used. Similarly, Int32 and Int64 are used for signed integer types.

Example:

var mark:Int = 70 
print(mark)  
mark = 80
print(mark) 

Output:

70
80 

Boolean

  • Swift Boolean data type stores only two values, either true or false.
  • The keyword “Bool” is used to declare the variable.
  • The default value is false.

Example:

let result: Bool = true  
print(result) 

Output:

true

Float

  • Swift float is used to store and represent the fractional values.
  • The keyword “ Float” is used to declare the variable.
  • The size of this data type is 32-bit.
  • The default value is 0.0.

Example:

let max_value: Float = 98.99  
print(max_value)

Output:

98.99

Double

  • Swift double data type is also used to store the fractional value, but it has more decimal points than float.
  • It uses the “Double” keyword to declare the values.
  • The default value is 0.0.
  • The size of this data type is 64-bit.

Example:

let more_points: Double = 39.009769651432
print(more_points)

Output:

39.009769651432

Character

  • Swift character data type represents and stores a single character string.
  • It uses the keyword “Character” to declare values.
  • It is also used to add emojis.

Example:

let your_name: Character = "G"  
print(your_name)

Output:

G

String

  • Swift string data type is used to store and represent the set of characters or a group of meaningful text.
  • It uses the keyword “String” to declare values.
  • The default value is “”.

Example:

let website_name: String = "Coding Ninjas"  
print(website_name)

Output:

Coding Ninjas

Range Table


Frequently Asked Questions

What is Optional in built-in data types in Swift?

Optional describes the variables which can hold a particular value or no value.

What are Tuples in built-in data types in Swift?

When we have to group multiple values in a single Compound Value, tuples are used.

Does Swift provide type inference?

Yes, Swift provides type inference, i.e., during compile time, the compiler automatically knows the type of data using the given values by the user.

Conclusion

In this article, we have extensively discussed the data types of Swift. We started with a brief introduction of Swift data types, their size, features, key points, and examples.

After reading about the data types of Swift, are you not feeling excited to explore more articles on the topic of Swifts? Don’t worry; Coding Ninjas has you covered. To learn, see Swift TuplesSwift recursionSwift methodsSwift StringsSwift ArraysSwift Dictionary, etc.

To study more about data types, refer to Abstract Data Types in C++.

Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Live masterclass