Labs ICT
โญ Pro Login

What is Swift?

Apple's modern language for building apps.

4 min read | Swift Tutorial
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

What is Swift?

Swift is Apple's powerful programming language designed for building apps on iOS, macOS, watchOS, and tvOS. Introduced in 2014, it quickly became the go-to language for Apple ecosystem development. What makes Swift special? It's open-source, incredibly fast, and built with safety in mind.

Before Swift, developers used Objective-C, which had a steeper learning curve and more room for errors. Swift brought modern syntax, type safety, and memory safety to the table. Companies like Airbnb, LinkedIn, and Netflix rely on Swift for their iOS applications. If you're looking to build apps for Apple platforms, Swift is your language.

// Your first Swift code
print("Hello, World!")

Why Learn Swift?

There are several compelling reasons to learn Swift in today's tech landscape:

  • High demand: iOS developers are among the highest-paid in the industry, and companies are constantly looking for Swift talent.
  • Growing ecosystem: Swift isn't just for iOS anymore. With server-side Swift frameworks like Vapor, you can build backends too.
  • Modern language features: Swift offers optionals, generics, closures, and protocol-oriented programming โ€” concepts that make your code safer and more expressive.
  • Open source: Since 2015, Swift has been open source, meaning you can contribute to the language itself and use it on non-Apple platforms.
  • Playgrounds: Apple's Swift Playgrounds app lets you learn and experiment with code on iPad, making it accessible even without a computer.

Variables and Constants

Swift uses let for constants (values that never change) and var for variables (values that can be updated). Prefer let whenever possible โ€” it makes your code safer and signals intent clearly.

let name = "Alice"        // constant โ€” cannot be changed
var age = 25              // variable โ€” can be updated
age = 26                  // this works
// name = "Bob"           // this causes a compile error

Swift is type-safe, which means every variable has a type that's determined at compile time. You can explicitly declare a type or let Swift infer it:

let explicit: Double = 42.0
let inferred = 42.0       // Swift infers this is a Double

let text: String = "Hello"
let automatic = "World"   // Swift infers this is a String

Data Types

Swift provides several built-in data types you'll use frequently:

  • Int โ€” whole numbers like 0, 42, -7
  • Double โ€” decimal numbers like 3.14, -0.5
  • String โ€” text like "Hello, Swift!"
  • Bool โ€” true or false values
  • Array โ€” an ordered collection like [1, 2, 3]
  • Dictionary โ€” key-value pairs like ["name": "Alice"]
  • Set โ€” an unordered collection of unique values
  • Tuple โ€” a fixed-size group of values with mixed types
let scores = [88, 92, 75, 100, 63]
let average = Double(scores.reduce(0, +)) / Double(scores.count)
print("Average score: \(average)")

Control Flow

Swift uses if, switch, and loops to control program flow. The switch statement in Swift is especially powerful โ€” it supports pattern matching, ranges, and tuples:

let grade = 85

switch grade {
case 90...100:
    print("Excellent")
case 80..<90:
    print("Great job")
case 70..<80:
    print("Good")
case 60..<70:
    print("Needs improvement")
default:
    print("Keep trying")
}
// Prints "Great job"

For loops, Swift offers for-in to iterate over ranges, arrays, and dictionaries:

for i in 1...5 {
    print("\(i) squared is \(i * i)")
}

let fruits = ["Apple", "Banana", "Cherry"]
for fruit in fruits {
    print("I like \(fruit)")
}

Functions

Functions in Swift are defined with the func keyword. They can accept parameters, return values, and use named arguments for clarity:

func greet(name: String) -> String {
    return "Hello, \(name)! Welcome to Swift."
}

let message = greet(name: "Alice")
print(message)

// Functions with default parameters
func power(base: Int, exponent: Int = 2) -> Int {
    var result = 1
    for _ in 0..<exponent {
        result *= base
    }
    return result
}

print(power(base: 3))       // 9
print(power(base: 2, exponent: 10))  // 1024

Swift also supports multiple return values using tuples, making it easy to return more than one value from a function without creating a custom struct.

๐Ÿงช Quick Quiz

What is Swift?