Swift Basic Data Types

In this article, I will explain Swift basic data types.

How to Specify Data Types for Swift Variables

In Swift, you can explicitly specify the data type when defining a variable, or you can omit the type and let Swift infer it from the assigned value.

For details, please refer to the Swift Variables page, but to explicitly specify the data type when defining a variable, use the following syntax:

  • var variableName: DataType = value
  • var variableName: DataType

If you do not specify the data type, the type will be inferred from the assigned value:

  • var variableName = value

Unlike languages such as Python, once a variable's data type is determined, it cannot be changed. If you try to assign a value of a different type, you will get an error.

For example, if you define a variable named number and assign it the value 1, its data type will be Int. If you then try to assign the string "2", you will get an error because an Int cannot store a String.

var number = 1
number = "2"

Swift Basic Data Types 1


Swift Basic Data Types

Swift provides the following basic data types:

(The examples show how the type is inferred when assigning a value without explicitly specifying the data type.)


  • Int - Integer (ex. value = 1)
  • Double - 64-bit floating-point number (ex. value = 1.5)
  • Float - 32-bit floating-point number
  • Bool - Boolean (ex. value = true)
  • String - String (ex. value = "Hello")
  • Array - Array (ex. value = ["apple", "orange", "banana"])
  • Set - Set
  • Dictionary - Dictionary (ex. value = ["make": "Toyota", "year": "2020"])

If you assign a floating-point value without specifying a type, Swift will infer it as Double, not Float. Therefore, if you want to use a Float, you must explicitly specify the data type when defining the variable.

A Set is similar to an Array in that it stores a collection of values, but unlike an array, a set does not allow duplicate values and does not maintain order. Since the syntax of Set and Array looks the same, you must explicitly specify the type as Set when you want to use it.

I will explain these types in more detail later, but for now, just keep in mind that these are the commonly used basic data types in Swift.