Skip to content
All essays
iOSMarch 27, 202510 min

Swift Fundamentals: Getting Started with Modern iOS Development

Learn Swift programming language basics. Variables, data types, control structures, and functions.

Ü
Ümit Uz
Mobile & Full Stack Developer

Swift is a modern, safe, and fast programming language developed by Apple. It's used for developing iOS, macOS, watchOS, and tvOS applications.

Variables and Constants

In Swift, var keyword is used for variables, let keyword for constants:

swift
// Variable
var message = "Hello"
message = "World" // ✅ Valid

// Constant
let pi = 3.14159
pi = 3.14 // ❌ Error: Cannot change constant

Data Types

Swift is a type-safe language with a strong type system:

swift
// Basic Types
let age: Int = 28
let price: Double = 19.99
let name: String = "Ahmet"
let isActive: Bool = true

// Collection Types
var numbers: [Int] = [1, 2, 3, 4, 5]
var cities: Set<String> = ["Istanbul", "Ankara", "Izmir"]
var info: [String: Any] = [
    "name": "Mehmet",
    "age": 25,
    "active": true
]

Optionals

One of Swift's most powerful features is optionals:

swift
var optionalName: String? = "Ahmet"
var emptyOptional: String? = nil

// Optional binding
if let name = optionalName {
    print("Name: \(name)")
}

// Nil coalescing
let displayName = emptyOptional ?? "Guest"

Control Structures

swift
// If-else
let score = 85

if score >= 90 {
    print("Excellent!")
} else if score >= 70 {
    print("Good")
} else {
    print("Needs improvement")
}

// Switch
let category = "A"

switch category {
case "A":
    print("Excellent")
case "B", "C":
    print("Good")
default:
    print("Other")
}

// Loops
for i in 1...5 {
    print(i)
}

let names = ["Ahmet", "Mehmet", "Ayse"]
for name in names {
    print(name)
}

Functions

swift
// Simple function
func greet() {
    print("Hello!")
}

// Function with parameters
func greet(_ name: String) {
    print("Hello \(name)!")
}

// Function with return value
func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}

// Multiple return values
func minMax(_ array: [Int]) -> (min: Int, max: Int)? {
    guard let min = array.min(), let max = array.max() else {
        return nil
    }
    return (min, max)
}

Closures

swift
// Simple closure
let greet = {
    print("Hello!")
}

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

// Trailing closure
array.forEach { element in
    print(element)
}

// Shorthand parameter names
array.forEach { print($0) }

Classes and Structs

swift
// Struct
struct Person {
    let name: String
    var age: Int

    func introduce() {
        print("\(name), \(age) years old")
    }
}

// Class
class Car {
    var brand: String
    var model: String

    init(brand: String, model: String) {
        self.brand = brand
        self.model = model
    }

    func start() {
        print("\(brand) \(model) starting")
    }
}

// Usage
let person1 = Person(name: "Ahmet", age: 25)
person1.introduce()

let car1 = Car(brand: "BMW", model: "X5")
car1.start()

Best Practices

  1. 1Use let over var: Always use let when the value doesn't need to change
  2. 2Optional handling: Use guard for optional unwrapping
  3. 3Type inference: Don't specify types when not necessary
  4. 4Naming: Follow Swift naming conventions
  5. 5Error handling: Use do-catch blocks

Learning Swift is the first step in your iOS development journey!

Related essays

Next essay
SwiftUI Complete Guide: Modern iOS UI Development