Swift Programming Blog

Learn Swift, tips, tutorials, and best practices

Getting Started with Swift

Swift is Apple’s modern programming language for iOS, macOS, watchOS, and tvOS. It is fast, safe, and expressive. To start programming in Swift, you can use Xcode on macOS or Swift Playgrounds on iPad.

Example: Hello World in Swift:

import Foundation

print("Hello, Swift!")

Swift Optionals Explained

Optionals are a powerful feature in Swift that allows variables to have a value or be nil. Use optional binding with if let or guard let to safely access optional values.

var name: String? = "Alice"

if let unwrappedName = name {
    print("Hello, \(unwrappedName)")
}

Swift Closures Basics

Closures are self-contained blocks of functionality that can be passed around and used in your code. They are similar to lambdas or anonymous functions in other languages.

let greet = { (name: String) -> String in
    return "Hello, \(name)"
}

print(greet("Bob"))