Introduction to Swift
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?
print("Swift is a modern programming language")
print("It is safe, fast, and expressive")
Login to try C/C++/Java/PHP code in the editor
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
let goals = ["safety", "speed", "expressiveness"]
for goal in goals {
print("Design goal: \(goal)")
}
Login to try C/C++/Java/PHP code in the editor
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
let usedFor = [("iOS apps", "Instagram, Airbnb"), ("Backend", "Vapor servers"), ("Scripting", "automation tools")]
for (area, example) in usedFor {
print("\(area): \(example)")
}
Login to try C/C++/Java/PHP code in the editor
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
print("This code is compiled before it runs")
print("The compiler already checked it for errors")
Login to try C/C++/Java/PHP code in the editor
- Assuming Swift only works for building iPhone apps and cannot be used for plain command-line programs or servers.
- Confusing Swift with Objective-C; Swift is a completely different, newer language with its own syntax.
- Thinking Swift code must always run inside Xcode; it can also run from a terminal with the
swiftcommand.
- 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
swiftcommand, without needing a full app project.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: