Data Types in Swift

The Swift programming language already comes with some built-in data types. Not only Swift, but it is common in all programming languages to have some data types.

In the programming world, we need to work with different types of data that can be stored in variables. Variables are reserved locations in the memory that are used to store data or values.

In this tutorial, our main focus is the data types of Swift programming language. We will see examples we will take the help of examples so that it will become easier for us to understand in a better way.

What are data types?

In the field of computer science or computer programming, a data type is an attribute that is associated with a piece of data that tells our computer system how to interpret the value.

It is important to let our computer know about the data type so that it ensures the data has been collected in the appropriate format.

Data types in Swift programming language

Swift comes with some built-in data types. Below are given a list of some most frequently used basic data types:

  • Integer
  • String
  • Floating-point numbers or float data types
  • Double
  • Boolean
  • Character
  • Optional
  • Tuples

Below is the syntax in Swift to create a variable with a data type:

var variableName:<data type> = <optional initial value>

Below is an example to create an integer type variable in Swift programming:

var myInt: Int = 61

We can also set the value later after creating our variable with data type and it works as same as the above snippet:

var myInt: Int
myInt = 61

Now we are going to discuss each data type mentioned above in detail:


Integer type of data used to represent a whole number. It doesn’t have any fractional part. Integer data types can be negative or positive.

Examples: 5, -9, 8

In Swift, we use the Int keyword to create integer-type variables. Below is how we can do it:

// Create variable of integer type
var myInt: Int = 34

// Print to terminal

Run this code online

The output will be:


Note that, even if we don’t use the Int keyword, it will automatically detect the value and make the variable an integer type variable.

In Swift, we can also define 8, 32 and 64 bit signed integers using Int8, Int32, and Int64 keywords. You can also use Unsigned integers with the UInt keyword. Using UInt8, UInt32, UInt64 you can create variables of 8, 32 and 64 bit of unsigned integers.

Below are examples of creating variables of different types of signed integers:

// Create 8 bit integer type variable
var myInt8: Int8 = 67

// Create 16 bit integer type variable
var myInt16: Int8 = 67

// Create 32 bit integer type variable
var myInt32: Int32 = 67

// Create 64 bit integer type variable
var myInt64: Int64 = 67

In the example below, you can see we have created different types of unsigned variable types:

// Create 8 bit Unsigned integer type variable
var myInt8: UInt8 = 67

// Create 16 bit Unsigned integer type variable
var myInt16: UInt8 = 67

// Create 32 bit Unsigned integer type variable
var myInt32: UInt32 = 67

// Create 64 bit Unsigned integer type variable
var myInt64: UInt64 = 67


String data types are used to represent any textual data or information. It is consist of a set of characters.

The String keyword is used in Swift programming to create a string-type variable. See the example below:

var fullName: String = "Tony Stark"

And the output will be:

Tony Stark

In the above example, we have created a variable with the name fullName and set the type to String. We assigned a string value in this variable and then print it.

Float data types

In any type of programming language, float data types are used to store decimal and fractional numbers. The range of float data type is in a range from 1.2*10-38 to 3.4 * 1038 (~6 digits).

In Swift, the Float keyword is used to create a float type variable. Below is an example of creating a float variable in Swift:

var myFloat: Float = 2.74519




Just like float data type, double is also represents the decimal or fractional value of numbers. But unlike float, double can hold decimal data up to 15 decimal places.

In Swift, the Double keyword is used to create double-type variables.

In the example below, we are creating a variable to store double type value:

var piValue: Double = 3.14159265358979

And the output of the above program will be:



A Boolean type of data represents the boolean value that may be true or false. It is used to represent the logical entity in the app or program.

The Bool the keyword is used in Swift to create a variable type of boolean.

Below is an example that tells us if a user is verified or not using the Boolean data type:

var isUserVerified: Bool = true



Another example with boolean value false

var isUserVerified: Bool = false




This data type is used to represent a single character literal. It is widely used to represent non-English languages and emojis.

Below is an example:

var englishLetter: Character = "F"

And output:


Below is another example where we are printing the flag of the United States:

var usFlag: Character = "\u{1F1FA}\u{1F1F8}"

The output is:



Optional type in Swift handles the absence of value in a variable and lets the program know if the variable has the value or not.

Below is the example where we declare an optional integer in Swift:

var maybeInt: Int?

And below we are declaring an optional String:

var maybeString: String?

Below is the equivalent of the above snippet:

var maybeString: String? = nil

To understand Optional type in a better way and how it works below is another example:

var checkStr:String? = nil

if checkStr != nil {
} else {
   print("checkStr has the value nil")

After we run the above program, below is the output that we can see in the console:

checkStr has the value nil


A tuple is a group of multiple values stored in a single compound value. There is no need to be the values in a tuple same type. Values in the tuple can be both the same or different in type.

In Swift, there creating a variable of a tuple type is quite easy. Below is the syntax of doing so:

var NameOfTuple = (Value1, value2, value3,......, varN)

Below is an example of a tuple type variable that contains the same type of data which is the string:

var colors = ("red","green","blue","black")


("red", "green", "blue", "black")

Below is another example of a tuple variable with all the variables integers:

var marks = (37,43,31,47,39)



Another example of a tuple is given below, that contains multiple types of value:

var mixedType = ("green",57,"yellow",3.14,5.98376647)


("green", 57, "yellow", 3.14, 5.98376647)