TExcelTAD
Home › Swift › Swift Get Started

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.

swift · try it yourself
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.

  1. Open the App Store on your Mac and install Xcode.
  2. Start Xcode and choose File ▸ New ▸ Project….
  3. Pick macOS ▸ Command Line Tool to practise the language, or iOS ▸ App to build an iPhone app.
  4. 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:

bash · try it yourself
swift --version
Swift version 6.3 (swift-6.3-RELEASE)
Target: x86_64-unknown-linux-gnu

Run a single file

Save the following code as hello.swift:

swift
let name = "Swift learner"
print("Hello, \(name)!")

and run it directly with the swift command:

bash · try it yourself
swift hello.swift
Hello, 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:

bash · try it yourself
mkdir Greeter && cd Greeter
swift package init --type executable
swift run
Creating 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:

text
Greeter/
├── Package.swift        ← package description (name, dependencies, targets)
└── Sources/
    └── main.swift       ← your program starts here

main.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.

swift · try it yourself
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?

  1. swift build-run
  2. swift run
  3. swiftc --go
  4. xcode run

In a Swift package, which file may contain top-level code that runs when the program starts?

  1. Package.swift
  2. app.swift
  3. main.swift
  4. Any file

Which tool do you need to build iPhone apps?

  1. Android Studio
  2. Xcode on a Mac
  3. Visual Studio
  4. Only the swift.org toolchain