Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 146 ยท Getting Started

Introduction to Swift

Meet Swift, the safe, fast, modern language behind iPhone apps, servers and embedded chips. Learn its history, where it runs and what sets it apart.

Swift is a general-purpose, compiled programming language. Apple introduced it in 2014 as a friendlier, safer successor to Objective-C, and since December 2015 it has been open source. Today it is the main language for apps on iPhone, iPad, Mac, Apple Watch and Apple Vision Pro, but it is not limited to Apple hardware: Swift also runs on Linux, Windows and Android, powers web servers, and has a trimmed-down mode for tiny microcontrollers.

This course teaches modern Swift 6. You do not need to know any other language. Every example on this page and in the following lessons is real Swift that is compiled on our server when you press Run, so you can change anything and see what happens. Swift has strong opinions about safety, and the compiler will often stop you with an error message. Reading those messages is a skill we practice on purpose, starting with this lesson.

What makes Swift different

Languages are collections of trade-offs. These are the ones Swift made.

Safe by default. Variables must be given a value before use, integers do not silently overflow, array indexes are checked, and a value that might be missing has a special type (an optional) that forces you to deal with the "nothing there" case. Whole classes of bugs, such as null pointer crashes, are caught by the compiler instead of by your users.

Fast. Swift compiles to native machine code through the LLVM compiler infrastructure. There is no virtual machine and no garbage collector pausing your program. Memory is managed with automatic reference counting (ARC), which frees objects at predictable moments. Performance is in the same league as C++ and Rust for most code.

Expressive and concise. Type inference means you rarely write types that the compiler can work out. Closures, pattern matching, generics and protocols let you write code that reads close to the idea behind it.

Value types first. Swift encourages small, copy-on-assignment types (struct and enum) over shared mutable objects. Most of the standard library, including Array, String and Dictionary, are structs. That makes programs easier to reason about, and in Swift 6 it helps the compiler prove that concurrent code has no data races.

Modern concurrency. async/await, structured concurrency, actors and the Sendable protocol are part of the language, and Swift 6 turns data-race mistakes into compile-time errors.

Your first taste of Swift

Do not worry about understanding every word yet. Read this program once, run it, then change a value.

Swift A small tour of the syntax

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

You can already see a few ideas. let creates a constant and var creates a variable (lesson: Constants and Variables). There are no semicolons and no parentheses around conditions. \(...) puts any value inside a string (see String Interpolation and Formatting). The array features was created without us naming its type: the compiler worked out that it is an array of strings.

Safety in action: the compiler as a teammate

Here is a program that tries to put text into a number. In many languages this compiles and fails later, or turns into something strange. Swift refuses to build it.

Swift Type mismatch is a compile error (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

The compiler reports an error similar to this:

TEXT
main.swift:2:9: error: cannot assign value of type 'String' to type 'Int'

The first number is the line, the second the column, and the message explains the problem. score was inferred as an Int on line 1 and an Int can never hold a String. Nothing ran, so nothing could go wrong. The fix is to make the types agree: assign 100, or choose a different variable for the text. Compile errors are your friend; each one is a bug that never reached a user.

Optionals: "there might be no value"

Swift does not have a null that any variable can silently hold. If a value may be absent, its type says so with a question mark, and you must unwrap it before use.

Swift Parsing might fail

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Int("42") does not return an Int; it returns an Int? ("an optional Int") because the text might not be a number. if let opens the box only if it holds a value, and ?? supplies a default when it is empty. You will meet optionals properly in Optionals and Optionals in Depth.

Value types and reference types

A struct is copied when you assign it. A class is shared. This single difference explains a lot of Swift design.

Swift Copy versus share

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Changing b did not affect a, because b is its own copy. Changing d changed c as well, because both names refer to the same object. Swift programmers default to structs, and reach for classes when identity or shared state is exactly what they want. See Structs, Classes and Value vs Reference.

Where Swift runs

  • Apple platforms. iOS, iPadOS, macOS, watchOS, tvOS and visionOS. You build apps with Xcode and frameworks such as SwiftUI. These frameworks are not available in this course's in-browser runner (see Swift on Apple Platforms and SwiftUI Overview, which are read-only).
  • Linux and Windows. Official toolchains exist for both. Linux is the usual home of Swift on the server (see Swift on the Server and Swift on Linux and Windows). Our runner uses Linux.
  • Android and WebAssembly. Community workgroups maintain support. This is progressing quickly, but it is less mainstream than Apple, Linux and Windows.
  • Embedded. A restricted subset called Embedded Swift targets microcontrollers (see Embedded Swift).

All of this is the same language. The standard library and the language rules are identical everywhere; what differs is the frameworks around them.

A short history

  • 2010-2014. Chris Lattner starts the project at Apple. It is announced at WWDC in June 2014.
  • 2015. Swift becomes open source, with a public evolution process where anyone can propose language changes (see Swift Evolution).
  • 2016. Swift 3 renames much of the standard library to follow consistent API design guidelines. It was the last big source-breaking release.
  • 2019. Swift 5 brings ABI stability on Apple platforms: compiled Swift libraries from different compiler versions can work together.
  • 2021. Swift 5.5 adds async/await and actors.
  • 2024. Swift 6 adds a language mode that checks data races at compile time, typed throws and more.
  • Since then. Releases arrive about twice a year. This course uses the Swift 6.3 toolchain.

Older tutorials may show code from Swift 2, 3 or 4. The ideas are the same but many names changed. If something you find online does not compile, check which Swift version it was written for.

What you need to start

For this course: nothing. Your browser sends each program to a server that compiles and runs it. If you later want to build real apps, install Swift from swift.org (any platform) or Xcode (Mac). On your own machine the compiler is invoked as swiftc or via swift run; see Your First Swift Program and How Swift Works.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the type of Int("42") in Swift?

    Parsing can fail, so the initializer returns an optional Int (Int?) that is nil when the text is not a number.
  2. 2. What happens when you assign a String to a variable that was inferred as Int?

    Swift is statically typed and has no implicit conversions between unrelated types, so this is rejected at compile time.
  3. 3. After var b = a and b.x = 99, where a and b are structs, what is a.x?

    Structs are value types: assignment copies the value, so changing b leaves a untouched.
  4. 4. Which statement about Swift's memory management is correct?

    Swift compiles to native code and uses ARC to release class instances at predictable points.
  5. 5. Which of these is NOT a place where Swift runs?

    Swift is a compiled language for many platforms, not just a browser language. It can target WebAssembly, but is not limited to it.

Summary

  • Swift is a safe, fast, compiled language created by Apple in 2014 and open source since 2015.
  • It runs on Apple platforms, Linux, Windows, and in server and embedded settings.
  • Safety comes from static types, optionals, checked arithmetic and (in Swift 6) data-race checking.
  • Structs and enums are value types, classes are reference types; prefer values by default.
  • This course uses Swift 6.3, compiled on our server; compiler errors are part of learning.

Next: Your First Swift Program