Swift Get Started
You can start writing Swift right now in the playgrounds on this site. When you want to build real apps or work offline, you need the Swift toolchain on your own computer. This chapter shows the options.
Option 1: The playgrounds on ExcelTAD
Every swift example in this course has a ▶ Run button. Your code is sent to a server, compiled with Swift 6.3 and the output is shown below the editor. Nothing has to be installed.
print("Swift is running!")
print(2 + 3)Swift is running!
5
Option 2: Xcode on a Mac
Xcode is Apple's free development environment. It contains the Swift compiler, the iOS simulator, the SwiftUI preview canvas and the tools to publish apps on the App Store.
- Open the App Store on your Mac and install Xcode.
- Start Xcode and choose File ▸ New ▸ Project….
- Pick macOS ▸ Command Line Tool to practise the language, or iOS ▸ App to build an iPhone app.
- Press ⌘R to build and run.
Tip: Xcode also has Playgrounds (File ▸ New ▸ Playground) — files that re-run your code as you type and show each value in a side bar. They are great for experimenting.
Option 3: The command line (macOS, Linux, Windows)
Download the toolchain from swift.org/install (on macOS it comes with Xcode). Then check the version:
swift --versionSwift version 6.3 (swift-6.3-RELEASE)
Target: x86_64-unknown-linux-gnu
Run a single file
Save the following code as hello.swift:
let name = "Swift learner"
print("Hello, \(name)!")and run it directly with the swift command:
swift hello.swiftHello, Swift learner!
Create a package
For anything bigger than one file, use the Swift Package Manager (SwiftPM). It creates a project folder, compiles your code and downloads libraries:
mkdir Greeter && cd Greeter
swift package init --type executable
swift runCreating executable package: Greeter
Creating Package.swift
Creating .gitignore
Creating Sources
Creating Sources/main.swift
Building for debugging...
[7/7] Applying Greeter
Build of product 'Greeter' complete! (2.31s)
Hello, world!
The generated folder looks like this:
Greeter/
├── Package.swift ← package description (name, dependencies, targets)
└── Sources/
└── main.swift ← your program starts heremain.swift is special: its top-level code is the entry point of the program. In other files you may only declare things (functions, types, …).
Which option should I use?
| Goal | Best choice |
|---|---|
| Learn the language with this course | The playgrounds on this site |
| Build iPhone, iPad or Mac apps | Xcode on a Mac |
| Write tools or servers on Linux/Windows | Toolchain from swift.org + swift run |
| Quick experiments with live results | Xcode Playgrounds or the playgrounds here |
Editing the code
Try changing the example below so it prints your own name and favourite number, then run it.
let myName = "Taylor"
let favouriteNumber = 7
print("My name is \(myName) and my favourite number is \(favouriteNumber).")My name is Taylor and my favourite number is 7.
Note: Building iOS apps requires a Mac with Xcode. Everything else in the Swift Tutorial, Swift Types and Swift Advanced sections runs on any platform.
Test yourself
Which command builds and runs a Swift package from the terminal?
- swift build-run
- swift run
- swiftc --go
- xcode run
In a Swift package, which file may contain top-level code that runs when the program starts?
- Package.swift
- app.swift
- main.swift
- Any file
Which tool do you need to build iPhone apps?
- Android Studio
- Xcode on a Mac
- Visual Studio
- Only the swift.org toolchain