Before you can build apps, you need to understand how computers handle data at the most fundamental level. This chapter covers the building blocks: how machines process numbers, how you perform calculations in Swift, and how you store and name data using constants and variables.
Open a new Playground in Xcode and follow along. Every code example in this chapter is designed to run in a playground.
How Computers Actually Think
A computer is, at its core, a math machine. The brain of the computer is the Central Processing Unit (CPU). It does one thing extremely well: perform arithmetic operations on numbers, millions of times per second.
The CPU works with small memory slots called registers. When it needs data, it pulls numbers from the computer's main memory (RAM) into registers, runs calculations, and writes results back. That's the entire cycle. Every app you've ever used -- from Safari to Instagram -- is built on this simple loop of reading, calculating, and writing numbers.
Everything on your screen is a number. Text? Each character maps to a number (the letter "A" is 65). Images? Each pixel is three numbers: how much red, green, and blue. Sound? A sequence of numbers representing air pressure over time. The CPU doesn't know what any of it means -- it just does the math.
Number Systems: Binary and Hexadecimal
Why computers use binary
You count in base 10 (decimal) because you have ten fingers. Computers count in base 2 (binary) because, at the hardware level, a circuit is either on or off. Voltage present = 1. No voltage = 0. That's it -- two states, two digits.
Each binary digit is called a bit. Eight bits make a byte. A 64-bit CPU -- which is what every modern Mac and iPhone has -- can process numbers up to 18.4 quintillion in a single operation.
Hexadecimal: a shorthand for binary
Binary numbers get long fast. The number 255 in binary is 11111111. Writing 32-bit or 64-bit numbers in binary is impractical, so programmers use hexadecimal (base 16) as a compact shorthand.
Hex uses digits 0-9 plus letters a-f (where a=10, b=11, c=12, d=13, e=14, f=15). Each hex digit maps to exactly four binary digits, making conversion trivial.
You'll encounter hex in Swift when working with colors (0xFF0000 for red) and memory addresses.
Understanding binary and hex helps you appreciate what happens under the hood, but you won't manually convert numbers in everyday Swift programming. The compiler handles all of that. Think of this as background knowledge that makes you a better developer.
From Code to CPU: How Swift Gets Executed
Writing individual CPU instructions by hand would be impossibly tedious. Instead, you write source code in Swift, and a program called the compiler translates it into the tiny machine instructions the CPU understands.
A single line of Swift might become dozens of machine instructions. The compiler also catches mistakes before your code runs -- type mismatches, missing values, syntax errors. This is why Swift is called a compiled language, and it's one of the reasons Swift code runs so fast.
Code Comments: Notes for Humans
Comments are text in your code that the compiler completely ignores. They exist for one reason: to help humans (including future you) understand what the code does and why.
// This is a single-line comment
/* This is a multi-line comment.
It can span multiple lines.
Great for longer explanations. */
/// This is a documentation comment.
/// Xcode uses these to generate help text.
Use comments to explain why you wrote something, not what it does. The code itself shows what. Comments should answer: why this approach? What's the context?
Arithmetic in Swift
Swift supports all the standard math operations you'd expect:
let sum = 5 + 3 // Addition: 8
let difference = 10 - 4 // Subtraction: 6
let product = 6 * 7 // Multiplication: 42
let quotient = 20 / 4 // Division: 5
let remainder = 14 % 3 // Modulo (remainder): 2
A few things to note:
- Swift follows standard math precedence: multiplication and division happen before addition and subtraction
- Use parentheses to force a different order:
(5 + 3) * 2gives 16, not 11 - Integer division truncates:
7 / 2gives3, not3.5. For decimal results, useDouble
Built-in Math Functions
Swift provides common math functions out of the box:
import Foundation
let squareRoot = sqrt(144.0) // 12.0
let power = pow(2.0, 10.0) // 1024.0
let maximum = max(5, 12) // 12
let minimum = min(5, 12) // 5
let absolute = abs(-42) // 42
Constants: Data That Doesn't Change
Use let to create a constant -- a value that's set once and never changes:
let pi = 3.14159
let appName = "My First App"
let maxLoginAttempts = 3
If you try to change a constant, the compiler stops you:
let score = 100
score = 200 // Error: Cannot assign to value: 'score' is a 'let' constant
This is a feature, not a limitation. Constants make your code safer because you know the value will never change unexpectedly. Use let by default -- only switch to var when you actually need to change a value.
Variables: Data That Does
Use var to create a variable -- a value that can change:
var score = 0
score = 10
score = score + 5 // score is now 15
Variables are essential for values that need to update: scores, counters, user input, network responses. But don't use var when let would work -- Swift and Xcode will even warn you if you declare a var but never mutate it.
Naming Your Data Well
Good names make code readable. Swift conventions:
- Use camelCase:
playerScore,isLoggedIn,maxRetryCount - Start with a lowercase letter
- Be descriptive:
let x = 5tells you nothing;let playerLives = 5tells you everything - Avoid abbreviations unless they're universally understood (
url,id,max) - Boolean names should read like questions:
isVisible,hasAccess,canSubmit
// Bad
let a = 42
var x = true
// Good
let userAge = 42
var isSubscribed = true
Compound Assignment Operators
Swift provides shorthand for updating a variable using its current value:
var health = 100
health += 10 // Same as: health = health + 10 → 110
health -= 25 // Same as: health = health - 25 → 85
health *= 2 // Same as: health = health * 2 → 170
health /= 2 // Same as: health = health / 2 → 85
Key Points
- Computers process everything as numbers. The CPU reads, calculates, and writes -- nothing more.
- Binary (base 2) is the computer's native number system. Hex (base 16) is a compact way to write it.
- The Swift compiler translates your source code into machine instructions.
- Use
letfor constants (values that don't change) andvarfor variables (values that do). - Default to
let. Only usevarwhen you need to mutate the value. - Name your data clearly using camelCase. Good names replace the need for comments.
In Chapter 4: Types & Operations, you'll learn about Swift's type system -- how the compiler knows what kind of data you're working with, and why that matters.
Ship your apps faster
When you're ready to publish your Swift app to the App Store, Simple App Shipper handles metadata, screenshots, TestFlight, and submissions — all in one place.
Try Simple App Shipper