Swift Data Types

In computer programming, data types specify the type of data that can be stored inside a variable. For example,

var num: Int

Here, Int is a data type that specifies that the num variable can only store integer data.

There are six basic types of data types in Swift programming.


Swift Data Types

Data Types Example Description
Character "s","a" a 16-bit Unicode character
String "hello world!" represents textual data
Int 3, -23 an integer number
Float 2.4, 3.14, -23.21 represents 32-bit floating-point number
Double 2.422342412414 represents 64-bit floating-point number
Bool true and false Any of two values: true or false

Swift Character

The character data type is used to represent a single-character string. We use the Character keyword to create character-type variables. For example,

// create character type variable
var letter: Character = "s"

print(letter)

// Output: s

In the above example, we have created a Character type variable named letter. Here, we have assigned "s" to the letter.

Note: If you try to assign more than one character like "abc" to variables of Character type, you'll get an error message.


Swift String

The string data type is used to represent textual data.

We use the String keyword to create string-type variables. For example,

// create string type variable
var language: String = "swift"

print(language)

// Output: swift

In the above example, we have created a String type variable named language. Here, we have assigned "swift" to the language.

To learn more about strings and characters, visit Swift Characters and Strings.


Swift Integer

An integer data type is used to represent a whole number with no fractional component. We use the Int keyword to create integer-type variables. For example,

// create integer type variable
var number: Int = 3

print(number)

// Output: 3

In the above example, we have created an Int type variable named number. Here, we have assigned 3 to the number.

Here are some of the basic properties of integers in swift programming.

  • Size: Depends on the platform type
  • Range: -231 to 231-1 (32 bit platform)
    -263 to 263-1 (64-bit platform)

Variants of Int type

Swift programming provides different variants of Int type having different sizes.

Variant Size Range
Int8 8 bit -128 to 127
Int16 16 bit -215 to 215-1
Int32 32 bit -231 to 231-1
Int64 64 bit -263 to 263-1
UInt Depends on platform 0 to 232(32-bit platform) 0 to 264(64-bit platform)

Swift Boolean

A boolean data type is used to represent logical entities. It can have one of two values: true or false. We use the Bool keyword to create boolean-type variables. For example,

// create boolean type variable

let passCheck: Bool = true
print(passCheck)

let failCheck: Bool = false
print(failCheck)

Output

true
false

In the above example, we have created Bool type variables: passCheck and failCheck. Here, we have assigned true to the passCheck variable and false to the failCheck variable.

If we don't assign any value to a boolean variable, it takes false as its default value.

Note: Booleans are frequently used with if-else statements. To learn more, visit Swift if...else Statement.


Swift Float

A float data type is used to represent a number with a fractional component. We use the Float keyword to create float-type variables. For example,

// create float type variable

let piValue: Float = 3.14
print(piValue)

// Output: 3.14

In the above example, we have created a Float type variable named piValue. Here, we have assigned 3.14 to piValue.

Here are some of the basic properties of float in swift programming.

  • Size: 32-bit floating-point number
  • Range: 1.2 x 10-38 to 3.4 x 1038 (Up to 6 decimal places)

Swift Double

Like Float, a double data type is also used to represent a number with fractional components.

However, Double supports data up to 15 decimal places. We use the Double keyword to create double variables. For example,

// create double type variable
let latitude: Double = 27.7007697012432

print(latitude)

// Output: 27.7007697012432

In the above example, we have created a Double type variable named latitude. Here, we have assigned 27.7007697012432 to latitude.

Here are some of the basic properties of double in swift programming:

  • Size: 64-bit floating-point number
  • Range: 2.3 x 10-308 to 1.7 x 10308 (Up to 15 decimal places)

Note: If we have a number like 27.7007697012432, we use:

  • Double to store the number with more precision (up to 15 decimal places)
  • Float to store the number with less precision (up to 6 decimal places)
Did you find this article helpful?