Tools
Log in
English

Learn to code

Learn Swift

Based on Swift 6.4

Swift structs, classes, optionals, protocols and SwiftUI app layout, by example.

1Classes and objectsStructs are copied when passed; classes are shared. Covers init, self and optionals (?, if let) with a packing example.Example: packing a suitcase2EncapsulationClose only the writing side with private(set), watch changes with willSet and didSet, and report bad requests with throws. A bank account example.Example: bank account3InheritanceOnly classes inherit. See override, super, final and Swift’s order of "child properties first, parent init second" in a workout log.Example: workout log4PolymorphismOne pin() call, different text for each place. Covers the protocol type any Place, is, as? and switch patterns with map pins.Example: pins on a map5AbstractionList the job in a protocol and put default behavior in an extension. See Swift’s protocol-oriented style, which works for structs too, with sensors.Example: reading sensors6Composition, static, ARCA team holding its players (composition), static let and static func, enums with associated values, ARC and weak, all in a futsal team.Example: futsal team7Access controlWhere the five levels open, public, internal, fileprivate and private reach, in one table and a lockable notebook. Pairs like public private(set) too.Example: lockable notebook+Project structurePick a command-line tool, a library (Swift package) or a SwiftUI app and get Package.swift, Sources and Tests, or an MVVM folder tree, as a zip.zip download

Suggested order

Start with classes and objects and go in order. Once the first topic has shown you structs versus classes and optionals (?), the later code reads much more easily. Only classes inherit; polymorphism and abstraction use protocols, which structs can join too.

Composition and static covers ARC and weak, and access control shows Swift-only levels such as open and fileprivate. Finish by building a Swift package or a SwiftUI app layout in project structure.

Where the output comes from

Every example was compiled in Swift 6 language mode with the Swift 6.4 toolchain on our server (the official Linux build) and run, and the output is pasted as it came out. Step-through values come from real runs too: we inserted a small reader on each marked line. Compiler errors in the mistake boxes are real errors with only the file name and line number removed.

SwiftUI screens run only on Apple devices, so those examples show a picture instead of output (on the project structure page). Swift does not run on this page; run your edited code on your own computer like this.

# paste the code into main.swift, thenswiftc main.swift -o demo./demo

Swift version

Text and examples follow Swift 6 language mode, and the server toolchain is 6.4. Swift 6 mode rejects code where several tasks could change the same value, so things that compiled in Swift 5, such as a plain static var, may now be errors (composition and static, mistake 2). Syntax added after 5.6 is marked at the top of the page, for example "Swift 5.9+".

Where Swift came from

Swift comes from Apple in the United States. Chris Lattner started it in July 2010; Apple announced it at WWDC in June 2014 and shipped Swift 1.0 that September. The source went public on swift.org in December 2015 under the Apache 2.0 license, together with a Linux port. Swift 5 (2019) made the ABI stable on Apple platforms, and Swift 6 (2024) catches code where several tasks touch the same value at compile time. The Apple-led Swift project, with its core team and steering groups for the language and platforms, decides each release; 6.4 came out in September 2026.

Apps for iPhone, iPad, Mac and Apple Watch are the main use, with screens built in SwiftUI. You can write servers (Vapor) and Linux command-line tools too, but fewer teams do. If the target is Android or Windows, other languages are more common.

On this site, begin with classes and objects, which covers structs versus classes and optionals. Project structure hands you a Swift package or a SwiftUI app (MVVM) skeleton to open in Xcode.

Tools that pair well Compare languages Swift project structure Learn C#

Made byApple (USA), started by Chris Lattner
First releaseAnnounced June 2014 · Swift 1.0 in September
Run today byThe Apple-led Swift project (swift.org, open source)
Used mostly foriPhone, Mac and Apple Watch apps, SwiftUI screens

Questions people ask

I am new to Swift. Where should I start?

Start with classes and objects and go in order. The pages assume you know basics such as variables, if and for. Coming from another language, the least familiar parts are usually structs versus classes, optionals and protocols.

Which version do the examples need?

They were compiled in Swift 6 language mode and run with the Swift 6.4 toolchain. A new Xcode project in Swift 6 mode runs them unchanged. Syntax added after 5.6 is marked at the top of each page.

Can I learn Swift without a Mac?

The language itself, yes: there are official toolchains for Linux and Windows, and these examples were run on Linux. Building iPhone screens with SwiftUI and shipping apps needs a Mac with Xcode.