Swift Cheat Sheet
Swift syntax, optional handling, structs versus classes, protocols, and closures for building type-safe iOS and macOS applications.
2 PagesIntermediateMar 28, 2026
Basic Syntax
Variables, control flow, and printing.
swift
import Foundationlet age = 30 // constant (let)var count = 0 // variable (var)let name = "Ada"let pi: Double = 3.14159if age >= 18 { print("\(name) is an adult")}for i in 0..<5 { print("Count: \(i)")}
Optionals
Safely handling the absence of a value.
swift
var middleName: String? = nil // optional, may hold nilif let name = middleName { // optional binding print("Middle name: \(name)")} else { print("No middle name")}let display = middleName ?? "N/A" // nil-coalescing operatorguard let unwrapped = middleName else { fatalError("middleName was nil")}
Protocols & Closures
Interfaces and inline functions.
swift
protocol Greetable { func greet() -> String}struct Person: Greetable { let name: String func greet() -> String { "Hello, \(name)" }}let numbers = [1, 2, 3, 4, 5]let doubled = numbers.map { $0 * 2 } // closure shorthandlet sum = numbers.reduce(0) { $0 + $1 } // 15
Core Keywords
Common Swift language keywords.
- let/var- constant and mutable variable declarations
- struct- value type, copied on assignment
- class- reference type, shared by reference
- protocol- defines a blueprint of methods/properties (like an interface)
- guard- early-exit control flow requiring a condition to hold
- extension- adds functionality to an existing type
- Optional (?/!)- ? declares optional, ! force-unwraps
- enum- value type supporting associated values and pattern matching
Pro Tip
Prefer struct over class by default in Swift — value semantics avoid unexpected shared mutable state; reach for class only when you need reference identity or inheritance.
Was this cheat sheet helpful?
Explore Topics
#Swift#SwiftCheatSheet#Programming#Intermediate#BasicSyntax#Optionals#ProtocolsClosures#CoreKeywords#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance