SyntaxStudy
Sign Up
Swift Beginner 8 min read

Introduction to Swift

Swift is Apple's powerful, open-source programming language for iOS, macOS, watchOS, and tvOS development. It combines C and Objective-C features while eliminating C compatibility constraints. Swift is safe, fast, and expressive.

Example
import Foundation

// Hello World
print("Hello, Swift!")

// Variables
let name = "Alice"    // constant
var age  = 30          // variable
age = 31

// String interpolation
print("\(name) is \(age) years old.")

// Arrays
var fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits.count)

// Dictionaries
var person = ["name": "Bob", "city": "London"]
person["language"] = "Swift"

// Optionals
var email: String? = nil
if let emailValue = email {
    print("Email: \(emailValue)")
} else {
    print("No email provided")
}

// Guard statement
func greet(_ name: String?) {
    guard let n = name else {
        print("No name"); return
    }
    print("Hello, \(n)!")
}