What Swift is
Swift is Apple's language for building apps on iPhone, iPad, Mac, Apple Watch, Apple TV and visionOS, and it is also an open-source general-purpose language that runs on Linux and Windows. It is compiled and statically typed, with a strong emphasis on safety: a value that might be missing is marked as optional, and the compiler makes you deal with that possibility before the program will build. Writing Swift feels modern and fairly compact, with type inference, string interpolation and readable control flow, while still producing fast native code.
Where Swift is used
- iPhone and iPad apps
- Native iOS apps are written in Swift, with the SwiftUI or UIKit frameworks for the interface and Xcode as the development environment.
- Mac, Watch, TV and Vision apps
- The same language and much of the same framework knowledge carry across Apple's other platforms, so a SwiftUI view written for iPhone can often be reused on the Mac or the watch.
- Server-side Swift
- Frameworks such as Vapor let teams write web APIs in Swift, sometimes so that the models and validation code can be shared with their iOS app.
- Command-line tools and scripts
- A single Swift file can be run directly from the terminal, and the Swift Package Manager builds larger command-line utilities.
- Embedded and cross-platform work
- Swift runs on Linux and Windows and has an Embedded Swift mode for microcontrollers, though these uses are newer and far less common than app development.
Your first Swift program
Saved as hello.swift. Run it with the steps in the next section.
let name = "Mateo"
let year = 2026
let items = ["tea", "bread", "rice"]
print("Hello, \(name).")
print("In five years it will be \(year + 5).")
print("You have \(items.count) items on your list.")What it prints
Hello, Mateo.
In five years it will be 2031.
You have 3 items on your list.- Lines 1 to 3 declare constants with
let. Aletvalue cannot change after it is set; usevarfor one that will. Swift infers the types (String, Int and [String], an array of strings) from the values. - Line 5 prints a line with
print, which adds a newline. Inside the string,\(name)is string interpolation: the value is inserted where the backslash and brackets are. - Line 6 shows that any expression can be interpolated, so
year + 5is worked out and placed in the sentence. - Line 7 reads the
countproperty of the array (a property, so no brackets) to find how many items there are. - There is no
mainfunction in a script file: Swift runs the top-level statements in order from the first line, which is why this file works as-is.
Run Swift on your own computer
On a Mac, Swift comes with Xcode and a single file can be run with one command. On Linux and Windows the toolchain from swift.org gives you the same compiler and command-line tools, but without Xcode and without Apple's app frameworks.
On macOS, install Xcode or the command-line tools
Xcode from the Mac App Store includes the Swift compiler, the simulators and everything needed to build apps. If you only want to run Swift files from the terminal, the much smaller Command Line Tools package is enough.
Shellxcode-select --installOn Linux or Windows, install the toolchain
Follow the install page on swift.org for your platform. It provides the compiler, the package manager and the standard library. SwiftUI and UIKit are not available there, so building an app for an Apple device still needs a Mac.
Check the version
Open a terminal and confirm the compiler is found. Any current 6.x release is suitable for everything on this page.
Shellswift --versionRun the file
Save the first program above as hello.swift. The swift command compiles and runs it in one step.
Shellswift hello.swiftCompile a standalone program
This produces an executable named hello that runs without the compiler present. For anything with more than one file, create a package with swift package init and build it with swift build.
Shellswiftc hello.swift -o helloBuild an app
Apps for iPhone and other Apple devices are made in Xcode: create a new project, choose SwiftUI, and the simulator lets you run it without a physical device. Xcode also has playgrounds for trying Swift interactively.
A learning order for Swift
Stages, not a timetable. Each one exists because the next would not make sense without it, and how long each takes depends on how much you write.
Stage 1. Constants, variables and output
- print and string interpolation
- let and var
- Int, Double, String and Bool
- type inference and annotations
- basic operators
Swift asks you to say whether a value changes, and the compiler holds you to it. The basic types and interpolation are enough to write small useful programs straight away.
Stage 2. Control flow
- if / else if / else
- switch with cases and ranges
- for-in over ranges and arrays
- while and repeat-while
- guard for early exit
Swift's switch is stricter and more powerful than in most languages, and guard is a habit worth forming early because it keeps the happy path unindented.
Stage 3. Optionals
- why a value can be nil
- declaring String? and Int?
- if let and guard let
- the ?? default operator
- optional chaining
- why force-unwrapping with ! is risky
Optionals are the concept that most separates Swift from other languages, and almost every framework call returns one. Understanding them properly makes the rest of Swift, and Apple's documentation, much easier to read.
Stage 4. Collections and functions
- arrays, dictionaries and sets
- map, filter and reduce
- func, parameters and argument labels
- closures
- error handling with throws, do and try
Argument labels are a distinctive part of Swift's readability, and closures are everywhere in app code, from button actions to network callbacks.
Stage 5. Structs, classes and protocols
- structs versus classes (value versus reference)
- properties and methods
- initialisers
- protocols and extensions
- enums with associated values
Swift leans on value types and protocols where other languages lean on class hierarchies. Knowing when a copy is made and when a reference is shared prevents a family of confusing bugs.
Stage 6. Apps and packages
- Xcode projects
- SwiftUI views and state
- navigation and lists
- Swift Package Manager
- tests with XCTest or Swift Testing
- a first app of your own
The language is now in place, so the app frameworks can be learned as frameworks rather than as more syntax. A first small app, built and tested, teaches more than any further reading.
Mistakes beginners make in Swift
- Using an optional as if it were a plain value
- Int("42") returns Int?, because the text might not be a number. Writing Int(text) + 1 fails with Value of optional type 'Int?' must be unwrapped to a value of type 'Int'. Unwrap it first, with if let n = Int(text) { ... }, or supply a default with Int(text) ?? 0.
- Force-unwrapping with ! to silence the compiler
- value! tells Swift you are sure there is something there. If there is not, the program stops with Fatal error: Unexpectedly found nil while unwrapping an Optional value. Reserve ! for cases where nil truly cannot happen and use if let, guard let or ?? everywhere else.
- Trying to change a let constant
- Cannot assign to value: 'total' is a 'let' constant means total was declared with let. Change it to var if it really varies. Swift also nudges the other way: a var that is never changed gets a warning suggesting let.
- Mixing Int and Double in arithmetic
- Swift never converts between number types silently. total / count with an Int and a Double stops with Binary operator '/' cannot be applied to operands of type 'Int' and 'Double'. Convert one side explicitly: Double(total) / count.
- Indexing a string with a number
- name[0] does not compile; the error says 'subscript(_:)' is unavailable: cannot subscript String with an Int. Swift strings are sequences of characters that can vary in size, so positions are String.Index values. For the first character use name.first; to work by position, convert with Array(name) or use prefix and dropFirst.
Strengths and trade-offs
Where it is strong
- Optionals and strict typing catch a large share of mistakes at compile time, and the error messages usually name the fix.
- It is the first-class language on every Apple platform, so the frameworks, documentation, sample code and tools are all designed around it.
- Compiles to native code, so programs are fast and run without a virtual machine or interpreter installed.
- Modern features such as value types, protocols, closures and structured concurrency with async/await make well-written Swift clear and safe.
Where it is not
- App development requires a Mac. The compiler runs on Linux and Windows, but Xcode, the simulators and the Apple frameworks (SwiftUI, UIKit) do not.
- Optionals, value versus reference types and strict typing mean more concepts to absorb before the first real app than a scripting language demands.
- Xcode is a large download, and the app frameworks change with each yearly release of iOS, so learning material goes out of date quickly.
- Outside Apple's platforms the ecosystem is thin: server-side and cross-platform Swift exist, but with fewer libraries and a smaller community than the established choices there.
Who Swift is for
Swift is the language to learn if you want to build apps for iPhone, iPad, Mac or Apple Watch and you have, or can get, a Mac to build them on. It is also a well-designed general-purpose language, so it is not a bad first language in itself, but be honest about the goal: almost all Swift learning material, jobs and libraries are about Apple apps. If you want Android, choose Kotlin. If you want both, learn one native language first and look at cross-platform frameworks afterwards. If you want the web, data work or scripting, Swift is the wrong tool.
Questions about learning Swift
- Do I need a Mac to learn Swift?
- To learn the language, no: the toolchain from swift.org runs on Linux and Windows, and everything in the roadmap above except the final stage works there. To build and run an iPhone or Mac app, yes. Xcode, the simulators and the SwiftUI and UIKit frameworks are only available on macOS, and building an app for the App Store requires Xcode.
- Do I need to learn Objective-C?
- No. Objective-C was Apple's app language before Swift, and you will still meet it in older projects and some documentation, but new apps are written in Swift and Swift can call Objective-C code when needed. Learn Swift first, and pick up enough Objective-C to read it only if a project you join uses it.
- What is the difference between Swift and SwiftUI?
- Swift is the language. SwiftUI is a framework, a library of views, layout and state-handling tools, for building user interfaces in Swift; UIKit is the older framework for the same job. Learn Swift the language first, then SwiftUI. Trying to learn both at once makes each harder, because a SwiftUI error is often really a Swift error.
- Can I make Android apps with Swift?
- Not in any practical sense. There is work on an Android SDK for Swift, but there is no official Android app framework for it, and Android's own frameworks are built for Kotlin and Java. If you need both platforms, learn Swift for iOS and Kotlin for Android, or use a cross-platform framework that targets both.
The primary source
When this guide and the official documentation disagree, the documentation is right and we would like to hear about it.