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 Sw…


This content originally appeared on DEV Community and was authored by Marcos Mendes

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"


This content originally appeared on DEV Community and was authored by Marcos Mendes


Print Share Comment Cite Upload Translate Updates
APA

Marcos Mendes | Sciencx (2023-02-28T13:27:24+00:00) Swift – Data Types Explained. Retrieved from https://www.scien.cx/2023/02/28/swift-data-types-explained/

MLA
" » Swift – Data Types Explained." Marcos Mendes | Sciencx - Tuesday February 28, 2023, https://www.scien.cx/2023/02/28/swift-data-types-explained/
HARVARD
Marcos Mendes | Sciencx Tuesday February 28, 2023 » Swift – Data Types Explained., viewed ,<https://www.scien.cx/2023/02/28/swift-data-types-explained/>
VANCOUVER
Marcos Mendes | Sciencx - » Swift – Data Types Explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/28/swift-data-types-explained/
CHICAGO
" » Swift – Data Types Explained." Marcos Mendes | Sciencx - Accessed . https://www.scien.cx/2023/02/28/swift-data-types-explained/
IEEE
" » Swift – Data Types Explained." Marcos Mendes | Sciencx [Online]. Available: https://www.scien.cx/2023/02/28/swift-data-types-explained/. [Accessed: ]
rf:citation
» Swift – Data Types Explained | Marcos Mendes | Sciencx | https://www.scien.cx/2023/02/28/swift-data-types-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.