Swift Cheat Sheet
Last updated
Hello World & basics
Swift needs no boilerplate at top level - a single line runs.
| Operation | Syntax |
|---|---|
| Print a line | print("Hello, World!") |
| Print without newline | print("Hi", terminator: "") |
| String interpolation | print("Hi \(name)") |
| Comment | // this is a comment |
| Multi-line comment | /* ... */ |
| Import a module | import Foundation |
| Run a file | swift run or swift main.swift |
Variables (let / var) & types
Use let for constants and var for mutable values; types are usually inferred.
| Operation | Syntax |
|---|---|
| Constant | let age = 30 |
| Variable | var count = 0 |
| Explicit type | let name: String = "Ada" |
| Basic types | Int, Double, String, Bool, Character |
| Type conversion | Double(i), String(n), Int("42") |
| Tuple | let pair = (1, "a") |
| Type alias | typealias ID = Int |
| Check type | value is String, value as? String |
Optionals
Optionals model the possible absence of a value; unwrap before use.
| Operation | Syntax |
|---|---|
| Declare optional | var name: String? = nil |
| Optional binding | if let n = name { ... } |
| Guard unwrap | guard let n = name else { return } |
| Nil-coalescing | let n = name ?? "default" |
| Optional chaining | user?.address?.city |
| Force unwrap (unsafe) | name! |
| Implicitly unwrapped | var name: String! |
| Map over optional | name.map { $0.count } |
Strings
Strings are value types with full Unicode support.
| Operation | Syntax |
|---|---|
| Length | s.count |
| Uppercase / lowercase | s.uppercased(), s.lowercased() |
| Concatenate | "foo" + "bar" |
| Interpolation | "Total: \(price)" |
| Contains | s.contains("ell") |
| Has prefix / suffix | s.hasPrefix("he") |
| Split | s.split(separator: ",") |
| Replace | s.replacingOccurrences(of: "a", with: "b") |
| Multi-line string | """ ... """ |
Collections (Array, Dictionary, Set)
Three core collection types, all value types.
| Operation | Syntax |
|---|---|
| Array literal | var nums = [1, 2, 3] |
| Append | nums.append(4) |
| Access / count | nums[0], nums.count |
| Map / filter | nums.map { $0 * 2 }, nums.filter { $0 > 1 } |
| Dictionary literal | var ages = ["Ada": 30] |
| Dictionary access | ages["Ada"] returns an optional |
| Set literal | var ids: Set = [1, 2, 3] |
| Insert into set | ids.insert(4) |
| Iterate dictionary | for (k, v) in ages { ... } |
Control flow
Conditions need no parentheses; switch must be exhaustive.
| Operation | Syntax |
|---|---|
| If / else | if x > 0 { ... } else { ... } |
| Ternary | let r = x > 0 ? "pos" : "neg" |
| Switch | switch n { case 1: ...; default: ... } |
| Switch with range | case 1...5: ... |
| For-in loop | for i in 0..<10 { ... } |
| For-in over array | for item in items { ... } |
| While loop | while x < 100 { ... } |
| Repeat-while | repeat { ... } while x < 100 |
| Break / continue | break, continue |
Functions & closures
Functions have labeled parameters; closures are self-contained code blocks.
| Operation | Syntax |
|---|---|
| Define a function | func add(a: Int, b: Int) -> Int { a + b } |
| External label | func greet(to name: String) { ... } |
| Default parameter | func greet(name: String = "World") { ... } |
| Variadic parameter | func sum(_ nums: Int...) -> Int { ... } |
| Multiple returns (tuple) | func bounds() -> (min: Int, max: Int) { ... } |
| Closure expression | let f = { (x: Int) -> Int in x * x } |
| Trailing closure | nums.map { $0 * 2 } |
| Shorthand arguments | { $0 + $1 } |
Structs vs classes
Structs are value types (copied); classes are reference types (shared).
| Operation | Syntax |
|---|---|
| Define a struct | struct Point { var x: Int; var y: Int } |
| Define a class | class Person { var name: String } |
| Class initializer | init(name: String) { self.name = name } |
| Create instance | let p = Point(x: 1, y: 2) |
| Mutating struct method | mutating func move() { x += 1 } |
| Class method | func greet() { ... } |
| Inheritance (class only) | class Student: Person { ... } |
| Computed property | var area: Int { width * height } |
| Static member | static let shared = Manager() |
Enums
Enums group related values and can carry associated data.
| Operation | Syntax |
|---|---|
| Define an enum | enum Direction { case north, south } |
| Use a case | let d = Direction.north |
| Switch on enum | switch d { case .north: ... } |
| Raw values | enum Status: Int { case ok = 200 } |
| Access raw value | Status.ok.rawValue |
| Init from raw value | Status(rawValue: 200) |
| Associated values | case result(Int, String) |
| Methods on enums | func label() -> String { ... } |
The Swift syntax you reach for most, on one page. This Swift cheat sheet is a quick reference for the core language - constants and variables, optionals, strings, collections, control flow, functions and closures, plus the structs, classes, and enums you build iOS and macOS apps with.
Everything here is standard Swift and compiles with the official toolchain. Copy what you need, or try every snippet live in the Swift playground - no Xcode required.
Swift cheat sheet FAQ
Is this Swift cheat sheet free?
How do I unwrap an optional in Swift?
nil, so you unwrap it before use. The safe ways are optional binding (if let n = name { ... }), a guard let at the top of a function, the nil-coalescing operator (name ?? "default"), and optional chaining (user?.name). Force unwrapping with name! works but crashes if the value is nil, so reach for it only when you are certain.What is the difference between a struct and a class in Swift?
struct is a value type: assigning or passing it makes a copy, so changes do not affect the original. A class is a reference type: copies share the same instance, and only classes support inheritance. Apple's guidance is to prefer structs by default and use a class when you need shared mutable state or inheritance.