Swift Fundamentals
Why Swift is designed the way it is
Swift was built around two goals: safety and clarity. “Safety” means the compiler catches whole classes of bugs before your app ever runs — for example, it won’t let you use a value that might be missing. “Clarity” means the code reads naturally. Keep these goals in mind; they explain many of Swift’s rules.
Constants and variables
Swift has two keywords for storing values, and choosing correctly is an important habit:
let— a constant. Its value can never change after it’s set. Prefer this.var— a variable that can change.
let name = "Anand" // cannot change
var score = 10 // can change
score = 20 // ok
// name = "Gaur" // ❌ compile error
Using let by default makes your intent clear and prevents accidental changes — the compiler even warns you if a var never actually changes.
Types and type inference
Every value in Swift has a type (String, Int, Double, Bool). You can write the type explicitly, but Swift usually infers it from the value, so you rarely need to.
let age: Int = 25 // explicit
let price = 99.5 // inferred as Double
let isActive = true // inferred as Bool
let letter: Character = "A"
Swift is strongly typed: you can’t accidentally mix a number and text. To combine them you convert explicitly, which prevents subtle bugs.
let count = 5
let message = "You have " + String(count) + " items"
// or, more cleanly:
let msg = "You have \(count) items" // string interpolation
Functions
A function groups reusable code. Swift functions have a distinctive feature: argument labels, which make calls read like sentences.
func greet(_ person: String, with greeting: String = "Hi") -> String {
return "\(greeting), \(person)!"
}
greet("Anand") // "Hi, Anand!"
greet("Priya", with: "Hello") // "Hello, Priya!"
Here _ means “no label for the first argument”, and with is an external label that makes the call read clearly. The -> String declares the return type, and = "Hi" is a default value.
Collections
Swift has three main collections, similar to other languages:
// Array — ordered list
var fruits = ["apple", "mango"]
fruits.append("kiwi")
// Dictionary — key/value pairs
var ages = ["Anand": 28, "Priya": 34]
ages["Ravi"] = 22
// Set — unique values
let unique: Set = [1, 1, 2, 3] // {1, 2, 3}
Transforming collections
Swift encourages a functional style with map, filter and reduce, where $0 refers to each element:
let nums = [1, 2, 3, 4, 5]
let evens = nums.filter { $0 % 2 == 0 } // [2, 4]
let doubled = nums.map { $0 * 2 } // [2, 4, 6, 8, 10]
let total = nums.reduce(0, +) // 15
Control flow
for fruit in fruits { print(fruit) }
if score > 10 { print("high") } else { print("low") }
switch score {
case 0..<50: print("Fail")
case 50...100: print("Pass")
default: print("Invalid")
}
Common mistakes
- Using
varwhenletwould do — prefer constants. - Trying to mix types without converting (Swift won’t allow it).
- Forgetting argument labels when calling functions.
Summary: Swift prizes safety and clarity. Useletby default, rely on type inference, and use argument labels, string interpolation, andmap/filter/reducefor clean, expressive code.