Skip to main content
Back to Journal
Programming

Swift Syntax Tips

Swift Syntax Quick Reference

Swift is Apple's powerful programming language for iOS, macOS, watchOS, and tvOS development. Here are essential syntax tips to get you up to speed.

Variables and Constants

var mutableVariable = "Can change"
let immutableConstant = "Cannot change"

// Type annotation
var name: String = "Bryan"
var age: Int = 30

Optionals

Optionals handle the absence of a value. They're one of Swift's most important features:

var optionalName: String? = nil

// Optional binding
if let name = optionalName {
    print("Hello, \(name)")
} else {
    print("No name provided")
}

// Guard statement
guard let name = optionalName else {
    return
}

Control Flow

// For-in loop
for i in 1...5 {
    print(i)
}

// Switch statement (no implicit fallthrough)
switch value {
case 1:
    print("One")
case 2, 3:
    print("Two or Three")
default:
    print("Other")
}

Functions

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

// Closures
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
SwiftiOSApple