Getting Started
4 snippetsYour 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 snippetsStore and manage data in Swift
Let (Constant)
let name = "John"Var (Variable)
var count = 0Type Annotation
let age: Int = 25Optional
var name: String? = nilArray
var fruits = ["apple", "banana", "cherry"]Dictionary
var ages = ["John": 30, "Jane": 25]Set
var unique: Set = [1, 2, 3]Operators
6 snippetsSymbols 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 = 4Range
1...5 // 1, 2, 3, 4, 5 (closed)
1..<5 // 1, 2, 3, 4 (half-open)Nil Coalescing
let name = optionalName ?? "Default" // unwrap or defaultTired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Loops
7 snippetsRepeat 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 < 5ForEach
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 snippetsDirect 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 snippetsHandle 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?.countFunctions
5 snippetsReusable 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 snippetsDefine 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 snippetsType-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 snippetsCatch and handle runtime errors
Do/Try/Catch
do {
let result = try riskyFunction()
} catch {
print(error)
}Try?
let result = try? riskyFunction()Throw
throw MyError.invalidInput