Featured image of post Swift in a Nutshell

Swift in a Nutshell


A Brief History of Swift

Once upon a time, in the magical land of Cupertino, Apple developers were stuck using Objective-C. Now, don’t get me wrong—Objective-C was powerful, but it had a syntax that looked like it came straight out of a horror movie from the 80s.

Then, in 2014, Apple decided, “You know what? We can do better.” And thus, Swift was born.

Key Milestones:

  • 2014: Swift 1.0 is released. Developers cheer. Objective-C starts packing its bags.
  • 2015: Swift goes open-source. The crowd goes wild!
  • 2016: Swift 3.0 simplifies syntax. Less typing, more happiness.
  • 2019+: Swift becomes the dominant language for iOS/macOS development, and it just keeps getting better.

Why Use Swift?

Great question! Let’s break it down:

🚀 Speedy & Safe

Swift is fast—like, Usain Bolt fast. It compiles to highly optimized code, and it prevents many common programming errors that used to make Objective-C developers cry themselves to sleep.

🏗️ Modern & Concise

Say goodbye to all that extra punctuation and boilerplate code! Swift is designed to be readable and easy to write. You can accomplish in a few lines what took a paragraph in Objective-C.

🌍 Cross-Platform

While Swift is mainly for iOS/macOS, it has also expanded to Linux, and there are even projects bringing it to Windows!

💡 Playgrounds & Xcode

Swift has a feature called Playgrounds, which lets you write and test code in real-time without running an entire project. It’s like a sandbox for programmers!


Let’s Write Some Swift Code!

Enough talking—let’s get our hands dirty.

1️⃣ Hello, World!

The “Hello, World!” of Swift is delightfully simple:

1
print("Hello, Swift World!")

Yep, that’s it. No semicolons required (unless you’re feeling nostalgic).


2️⃣ Variables & Constants

1
2
var myName = "ChatGPT" // A variable (can change)
let pi = 3.14159       // A constant (can't change)

Swift is type-safe, meaning it will yell at you if you try to assign an integer to a string variable. Good job, Swift.


3️⃣ Control Flow (If-Else, Loops)

1
2
3
4
5
6
7
let speed = 120

if speed > 100 {
    print("Slow down! You're going too fast!")
} else {
    print("You're good to go!")
}

For loops are also beautifully simple:

1
2
3
for i in 1...5 {
    print("Swift is awesome! Iteration: \(i)")
}

4️⃣ Functions

1
2
3
4
5
func greet(name: String) -> String {
    return "Hello, \(name)!"
}

print(greet(name: "Developer"))

Boom! A function that takes a name and returns a greeting. No unnecessary fluff.


5️⃣ Optionals (Because Null Pointers Are Evil)

Swift has a built-in safety net for missing values called optionals. Instead of crashing, it makes you handle missing values properly.

1
2
3
4
5
6
7
var myNumber: Int? = 42

if let number = myNumber {
    print("We have a number: \(number)")
} else {
    print("No number found!")
}

This way, you avoid those dreaded null pointer exceptions that haunt developers in other languages.


6️⃣ Structs & Classes

Swift loves structs. Unlike classes, they are value types (copied instead of referenced). But both are powerful!

1
2
3
4
5
6
7
struct Person {
    var name: String
    var age: Int
}

var person1 = Person(name: "Alice", age: 25)
print("Meet \(person1.name), who is \(person1.age) years old.")

7️⃣ Protocols (Like Interfaces, But Cooler)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
protocol Drivable {
    func drive()
}

struct Car: Drivable {
    func drive() {
        print("Vroom vroom!")
    }
}

let myCar = Car()
myCar.drive()

Swift uses protocol-oriented programming, which is a fancy way of saying “we love reusable blueprints.”


Key Ideas

ConceptSummary
Swift HistoryLaunched in 2014, replaced Objective-C
Speed & SafetyFaster than Objective-C, safer with type-safety
Modern SyntaxEasy-to-read, concise, no semicolons needed
OptionalsNo more null pointer crashes!
ProtocolsSwift loves reusable blueprints (protocol-oriented programming)
PlaygroundsTest Swift code in real-time

References