Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Swift Basics  ›  Lesson

Installing Swift

Installing Swift 12 minThe Swift language installs on Mac, Linux and Windows, but building SwiftUI screens for Apple devices needs a Mac with Xcode.

Installing Swift

Sprout's logic can be tried in minutes, but only once Swift is on your machine. The good news is that there is one obvious route per computer. The catch is that the Swift language and the SwiftUI screens do not have quite the same requirements.

The rule

Language anywhere; SwiftUI screens need a Mac with Xcode.

The Swift language installs almost everywhere:

  • Mac: install Xcode from the Mac App Store. It brings the Swift compiler, the iPhone simulator, and live SwiftUI previews.
  • Linux and Windows: download the Swift toolchain from swift.org using the installer for your system.
  • iPad or Mac, no setup: the Swift Playgrounds app lets you write and run Swift straight away.

Check that it worked from a terminal:

swift --version

Then run a single file of Swift directly:

swift sprout.swift

For anything bigger than one file, make a package and let Swift build and run it:

swift package init --type executable
swift run

SwiftUI is different. It is one of Apple's frameworks, so building and previewing SwiftUI screens for iPhone needs a Mac with Xcode. On Linux you can write and run plain Swift, but not an iPhone app.

The shortcut

Ask one question: am I writing logic, or drawing screens? Logic (Sprout's streak maths, its rules for a missed day) runs wherever Swift is installed. Screens need Xcode on a Mac.

Getting Sprout's code runningStep 1 of 4
  1. Decide what you are building today

    Today you only want to test streak rules. That is plain Swift logic.

The common mistake is expecting a Windows or Linux install to preview an iPhone screen. The compiler is the same language; the Apple frameworks are not there.

Drills

Answer fast — trust the rule.

quick drill

Which command checks that the Swift compiler is installed?

quick drill

You are on a Linux laptop. What can you do?

quick drill

What does installing Xcode on a Mac give you?

quick drill

Your Sprout logic has grown to several files. What is the usual command-line route?

Remember

  • Language anywhere; SwiftUI screens need a Mac with Xcode.
  • Ask: am I writing logic, or drawing screens?
  • swift --version checks; swift file.swift runs one file; swift run runs a package.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich machine for which job

A classmate has only a Windows laptop and wants to help with Sprout. Explain what they can and cannot do with Swift on that machine, and how they would check their install and run a file.