Variable Types in Swift

Mahi Garg
2 min readAug 25, 2023

--

Swift is a powerful and versatile programming language that allows developers to create robust and efficient applications across various platforms. One of the fundamental aspects of Swift is its strong type system, which ensures safety and reliability in code. In this blog, we will dive into the world of variable types in Swift, exploring the various categories and providing examples to enhance your understanding.

This post was originally posted at https://mahigarg.github.io/blogs/variable-types-in-swift/ and later reposted on Medium.

1. Integers

Integers are whole numbers with no fractional components. In Swift, there are different types of integers based on their size:

Examples:

a. Int

The Int type is the most commonly used integer type in Swift, and its size depends on the platform. On a 64-bit platform, Int is a 64-bit integer, while on a 32-bit platform, it’s a 32-bit integer.

var age: Int = 25

b. UInt

The UInt type represents an unsigned integer, meaning it can only hold non-negative values.

var numberOfParticipants: UInt = 100

c. Fixed-size Integers

Swift also provides fixed-size integer types like Int8, Int16, Int32, and Int64, which have a specific number of bits they can store.

var smallNumber: Int8 = 10

2. Floating-Point Numbers

Floating-point numbers represent numbers with fractional components. Swift offers two main types for floating-point numbers:

Examples:

a. Double

The Double type is a 64-bit floating-point number and provides a high level of precision.

var pi: Double = 3.1415926535

b. Float

The Float type is a 32-bit floating-point number with less precision compared to Double.

var temperature: Float = 25.5

3. Booleans

Please continue reading at https://mahigarg.github.io/blogs/variable-types-in-swift/

--

--