← Back to Swift Course | Chapter 1: Setup & Basics | Lesson 1 of 7

Introduction to Swift

Swift is a friendly programming language made by Apple that lets you tell a computer or phone exactly what to do, step by step.

What is Swift?

Swift is a general-purpose, compiled programming language created by Apple as a modern replacement for Objective-C. It combines the performance of a compiled language like C with the safety features of modern languages, such as strong typing and optionals that prevent an entire class of crashes.

Note: You don't need a Mac or Xcode to try basic Swift -- the open-source Swift toolchain runs on Linux too.

Example: What is Swift?

markup
print("Swift is a modern programming language")
print("It is safe, fast, and expressive")

Why Swift Was Created

Apple introduced Swift to fix long-standing pain points in Objective-C, such as unsafe pointers and verbose syntax. Swift adds strong type safety, optionals to handle missing values explicitly, and much cleaner syntax, while still being fast enough for demanding applications.

Example: Why Swift Was Created

markup
let goals = ["safety", "speed", "expressiveness"]
for goal in goals {
    print("Design goal: \(goal)")
}

Where Swift Is Used

Swift powers iOS, iPadOS, macOS, watchOS, and tvOS apps, but it has grown beyond Apple's platforms into server-side frameworks and general scripting thanks to its open-source toolchain. Companies use it for everything from mobile apps to backend services.

Example: Where Swift Is Used

markup
let usedFor = [("iOS apps", "Instagram, Airbnb"), ("Backend", "Vapor servers"), ("Scripting", "automation tools")]
for (area, example) in usedFor {
    print("\(area): \(example)")
}

Compiled, Not Interpreted

Unlike Python or JavaScript, Swift source code is compiled ahead of time into optimized machine code before it runs. This lets the compiler catch type errors and other mistakes before your program ever executes, and produces fast-running binaries.

Note: Running swift file.swift compiles and runs in one step, which is why it feels like scripting even though Swift is compiled.

Example: Compiled, Not Interpreted

markup
print("This code is compiled before it runs")
print("The compiler already checked it for errors")
Common Mistakes
  1. Assuming Swift only works for building iPhone apps and cannot be used for plain command-line programs or servers.
  2. Confusing Swift with Objective-C; Swift is a completely different, newer language with its own syntax.
  3. Thinking Swift code must always run inside Xcode; it can also run from a terminal with the swift command.
Chapter Summary
  • Swift is a modern, open-source, compiled language created by Apple and released in 2014.
  • It is designed to be safe (catching mistakes at compile time), fast (compiles to native machine code), and expressive.
  • Swift is used for iOS/macOS apps, but also for server-side code, command-line tools, and scripting.
  • Swift code can run as a standalone script with the swift command, without needing a full app project.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.