DEV Community

Cover image for Swift - Data Types Explained
Marcos Mendes
Marcos Mendes

Posted on • Updated on

Swift - Data Types Explained

Hello everyone, today I want talk a little bit about Data Types in Swift.

Data Types are the most important part of any programming language. Swift support six data types: Int, String, Float, Double, Bool and Character.

So let's start.

1. Int: In Swift, the integer is a data type that is used to represent a whole number with no fraction, for example, “2”, “56”, “3”, etc. Integer can be of 8, 16, 32, and 64 signed or unsigned bits. A variable of integer data type can be defined using the Int keyword. Int can be represented as a signed integer and UInt represents an unsigned integer.

Ex: var age: Int = 22

2. String: In Swift, the string is used to represent a sequence of characters or textual data, for example, “GeeksforGeeks”. The variable of string data type is defined using the String keyword.

Ex: var firstName: String = "Marcos"

3. Float: In Swift, the float data type is used to represent decimal numbers or we can say that float is used to represent 32-bit floating-point numbers, for example, “3.23”, “0.89753”, etc. It stores data with less precision. This data type represents up to 6 decimal places, so it is generally used when the floating-point number is less. If you want to store more decimal places then you can use a double data type. The variable of float data type is defined using the Float keyword.

Ex: var inputData: Float = 3.43

4. Double: Double data type is used to represent a number with fractional components or we can say that double data type is a 64-bit floating-point number, for example, “2.3345656”, “0.24354656”, etc. This data type represents up to 15 decimal places and it is generally used when the floating-point number is large. The variable of double data type is defined using the Double keyword.

Ex: var inputData: Double = 23.45788

5. Bool: In Swift, bool or boolean is used to represent logical entities. This data type has only two values that is true or false. Here the variable of boolean data type can be defined using the Bool keyword.

Ex: var isLoading: Bool = true

6. Character: In Swift, the character is used to represent a single letter or string literal. For example “G”, “E”, “K”, “S”, etc. A variable of character data type can be defined using the Character keyword.

Ex: var myInitial: Character = "M"

Top comments (1)

Collapse
 
jerusso profile image
John E Russo

Nice write up. 👍