in , ,

Swift Data Types

Swift Data Types
Swift Data Types

Swift Data Types: In this tutorial, you will learn about data types in Swift programming with the assistance of examples.

By and large, in any programming language, we use data types to store and control the data in variables dependent on the sort of data like integers, strings, characters, bool, and so on

At whatever point we create the variable in the swift programming language, consequently, the necessary space is reserved in memory for that variable, and the space assignment in memory is varied based on the sort of variables like integer or float or twofold, and so forth

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

var num: Int

Here, Int is a data type that indicates that the num variable can just store integer data.

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


Swift Data Types

Data TypesExampleDescription
Character“s”,”a”a 16-bit Unicode character
String“hello world!”represents textual data
Int3, -23an integer number
Float2.4, 3.14, -23.21represents 32-bit floating-point number
Double2.422342412414represents 64-bit floating-point number
Booltrue and falseAny of two values: true or false

Swift Character

The character data type is used to address a single character string. We use the Character watchword to create character-type variables. For instance,

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


// 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 attempt 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 address text based data.

We use the String watchword to create string-type variables. For instance,

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


// Output: swift

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

Swift Integer

An integer data type is used to address a whole number with no partial segment. We use the Int keyword to create integer sort variables. For instance,

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


// 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 a portion of the fundamental 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.

Int88 bit-128 to 127
Int1616 bit-215 to 215-1
Int3232 bit-231 to 231-1
Int6464 bit-263 to 263-1
UIntDepends on platform0 to 232(32-bit platform) 0 to 264(64-bit platform)

Swift Boolean

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

// create boolean type variable

let passCheck: Bool = true

let failCheck: Bool = 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.

In the event that 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 address a number with a fractional part. We use the Float catchphrase to create float-type variables. For instance,

// create float type variable

let piValue: Float = 3.14

// 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 a portion of the fundamental 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 twofold data type is additionally used to address a number with fractional segments.

Notwithstanding, Double supports data up to 15 decimal spots. We use the Double catchphrase to create twofold variables. For instance,

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


// 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 a portion of the fundamental properties of twofold 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)

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

HTML Paragraphs

HTML Paragraphs

HTML Styles

HTML Styles