Swift: Basic Syntax [Bools, Floats, and Doubles]

Bool
Bool is the Boolean data type. A Boolean can only ever hold one of two states: true or false.

Swift
var isDarkModeEnabled: Bool = false
isDarkModeEnabled.toggle() // Flips the state: false -> true

let isAuthenticated: Bool = true
let hasAdminPrivileges: Bool = false

print("Is the user logged in? : \(isAuthenticated)")       // true
print("Does user have admin access? : \(hasAdminPrivileges)") // false
Float and Double
Float and Double are used to store real numbers featuring fractional decimal pointsknown in computer science as floating-point numbers.

Swift offers two main variations:

Double: Represents a 64-bit floating-point number.

Float: Represents a 32-bit floating-point number.

In a standard 64-bit environment, a Double has a precision of at least 15 decimal places, whereas a Float maxes out at 6 decimal places. While you can choose either based on your memory constraints, the golden rule in Swift is to always default to Double unless you have a specific, highly optimized reason not to.

Swift
var preciseGPS: Double = 37.56653589793238
var roughGPS: Float = 37.56653589793238

print("Double preserves it: \(preciseGPS)") // 37.56653589793238
print("Float cuts it off:   \(roughGPS)")   // 37.566536 (Truncated and rounded at the 6th digit)
💡 Pro-Tip: Generating Random Numbers

Since Swift 4.2, you can generate random values natively using the .random(in:) method across both whole and decimal numbers:

Swift
let randomDiceRoll = Int.random(in: 1...6)
let randomOpacity = Double.random(in: 0.1...1.0)
let randomPitch = Float.random(in: -0.8...1.2)
Type Inference with Doubles and Bools
Alongside String and Int, Doubles and Bools make up the core daily diet of a Swift developer.

"Double" is short for Double-precision floating-point number. Whenever you type a decimal into your code without explicitly declaring its type, Swift's compiler automatically assigns it as a Double. The exact same logic applies to true or false:

Swift
var downloadSpeed = 45.8 // Automatically inferred as Double
var isConnected = true   // Automatically inferred as Bool
Why Do We Need Both Doubles and Integers?
Swift gives you several ways to store numbers to solve different engineering problems, but it strictly forbids you from mixing them together. Our two primary tools are:

Int (Integers): Whole numbers like 0, 42, or -100.

Double: Fractional numbers like 0.5, 3.14159, or -12.004.

Take a look at this scenario:

Swift
var downloadedFiles = 1     // Int
var progressPercentage = 1.0 // Double
Both variables technically represent the value of "one" right now. Naturally, you might ask: Why can't I just write var total = downloadedFiles + progressPercentage?

The answer is Type Safety. Swift refuses to guess your intentions. While 1 + 1.0 equals a clean 2.0, progressPercentage is a variable (var). In the next millisecond of the app running, it could change to 1.4.

If Swift allowed you to implicitly add an Int to a Double, the compiler would be forced to make a dangerous guess: Do I round 2.4 down to the integer 2 (destroying the user's data), or do I silently upgrade the integer to a decimal? To prevent invisible data loss, Swift forces you to manually bridge the gap:

Swift
var itemQuantity = 3     // Int
var unitPrice = 4.50     // Double

// WRONG: Will trigger a strict compiler error
// let totalCost = itemQuantity * unitPrice 

// CORRECT: You explicitly tell Swift to treat the Int as a Double for this math
let totalCost = Double(itemQuantity) * unitPrice

댓글

이 블로그의 인기 게시물

How to Open, Set, and Save Environment Variables (Windows, macOS, Linux)

How to Set Up Fastlane for iOS

How to Fix Auto Layout Constraint Conflicts and Spacing Issues (Question, Image, and Options)