AI Generate Swift docs instantly

Swift Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Your first steps with Swift basics

Hello World

print("Hello, World!")

Comments

// Single line comment

/* Multi-line
   comment */

String Interpolation

let name = "Swift"
print("Hello, \(name)!")  // Hello, Swift!

User Input

if let input = readLine() {
    print("You entered: \(input)")
}

Variables & Data Types

7 snippets

Store and manage data in Swift

Let (Constant)

let name = "John"

Var (Variable)

var count = 0

Type Annotation

let age: Int = 25

Optional

var name: String? = nil

Array

var fruits = ["apple", "banana", "cherry"]

Dictionary

var ages = ["John": 30, "Jane": 25]

Set

var unique: Set = [1, 2, 3]

Operators

6 snippets

Symbols for calculations and comparisons

Arithmetic

5 + 3    // 8 (addition)
5 - 3    // 2 (subtraction)
5 * 3    // 15 (multiplication)
5 / 3    // 1 (integer division)
5 % 3    // 2 (modulus)

Comparison

5 == 3   // false (equal)
5 != 3   // true (not equal)
5 > 3    // true (greater)
5 < 3    // false (less)
5 >= 3   // true (greater or equal)
5 <= 3   // false (less or equal)

Logical

true && false  // false (AND)
true || false  // true (OR)
!true          // false (NOT)

Compound Assignment

var x = 5
x += 3   // x = 8
x -= 2   // x = 6
x *= 2   // x = 12
x /= 3   // x = 4

Range

1...5    // 1, 2, 3, 4, 5 (closed)
1..<5    // 1, 2, 3, 4 (half-open)

Nil Coalescing

let name = optionalName ?? "Default"  // unwrap or default

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Loops

7 snippets

Repeat code efficiently with iterations

For-in Range

for i in 1...5 {
    print(i)  // 1, 2, 3, 4, 5
}

For-in Array

let fruits = ["apple", "banana"]
for fruit in fruits {
    print(fruit)
}

Enumerated

for (index, fruit) in fruits.enumerated() {
    print("\(index): \(fruit)")
}

While

var i = 0
while i < 5 {
    print(i)
    i += 1
}

Repeat-While

var i = 0
repeat {
    print(i)
    i += 1
} while i < 5

ForEach

fruits.forEach { fruit in
    print(fruit)
}

Break & Continue

for i in 1...10 {
    if i == 3 { continue }  // skip 3
    if i == 7 { break }     // stop at 7
    print(i)
}

Control Flow

3 snippets

Direct program execution with conditions

If/Else

if x > 0 {
    print("positive")
} else if x < 0 {
    print("negative")
} else {
    print("zero")
}

Guard

guard let name = optionalName else {
    return
}

Switch

switch day {
case "Mon":
    print("Monday")
case "Tue", "Wed":
    print("Midweek")
default:
    print("Other")
}

Optionals

4 snippets

Handle nil values safely in Swift

Optional Binding

if let name = optionalName {
    print(name)
}

Nil Coalescing

let name = optionalName ?? "Default"

Force Unwrap

let name = optionalName!

Optional Chaining

let count = person?.address?.street?.count

Functions

5 snippets

Reusable blocks of code with closures

Function

func greet(name: String) -> String {
    return "Hello, \(name)!"
}

Default Param

func greet(name: String = "World") -> String {
    return "Hello, \(name)!"
}

Argument Labels

func greet(to name: String) -> String {
    return "Hello, \(name)!"
}

Closure

let add = { (a: Int, b: Int) -> Int in
    return a + b
}

Trailing Closure

numbers.map { $0 * 2 }

Classes & Structs

4 snippets

Define custom types and objects

Struct

struct Person {
    var name: String
    var age: Int
}

Class

class Dog {
    var name: String

    init(name: String) {
        self.name = name
    }

    func bark() -> String {
        return "\(name) says woof!"
    }
}

Inheritance

class Puppy: Dog {
    func play() -> String {
        return "\(name) is playing!"
    }
}

Protocol

protocol Greetable {
    func greet() -> String
}

Enums

3 snippets

Type-safe enumerated values

Basic Enum

enum Direction {
    case north, south, east, west
}

Associated Values

enum Result {
    case success(String)
    case failure(Error)
}

Raw Values

enum Status: Int {
    case pending = 0
    case active = 1
}

Error Handling

3 snippets

Catch and handle runtime errors

Do/Try/Catch

do {
    let result = try riskyFunction()
} catch {
    print(error)
}

Try?

let result = try? riskyFunction()

Throw

throw MyError.invalidInput

More Cheat Sheets

FAQ

Frequently asked questions

What is a Swift cheat sheet?

A Swift cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Swift. It helps developers quickly look up syntax without searching through documentation.

How do I learn Swift quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Swift concepts?

Key Swift concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Swift code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Swift code using AI.

Code Conversion Tools

Convert Swift to Other Languages

Easily translate your Swift code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Swift Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week