Development Environment Setup

Article · 2:00

Install Xcode, find your way around it, and run your first Swift in a Playground and on the Simulator.

Xcode is the only place iOS apps get built, and it is large - around 40 GB installed with one simulator runtime. Install it from the Mac App Store, open it once, and let it finish installing the additional components it asks for before you do anything else. That first launch does work that a later one will not repeat.

The window has four regions and it is worth naming them now, because every instruction you read elsewhere assumes you know them. The navigator on the left lists your files. The editor in the middle is where you type. The inspector on the right shows the attributes of whatever is selected. The debug area at the bottom holds the console, which is where print output appears and where crashes explain themselves.

There are two ways to run Swift, and beginners should use both. A Playground executes code as you type and shows the result of every line in a margin, which makes it the right place to learn the language: no app, no screens, no build step. The Simulator is a full iPhone running on your Mac, and it is where you run an actual app. The Simulator is not an emulator of the hardware - it runs your code compiled for your Mac's own processor - so it is fast, but it cannot tell you everything. The camera, the accelerometer and real network conditions need a device.

Create a Playground now: File, New, Playground, choose Blank, and save it somewhere you will find again. Type the code below and watch the right-hand margin.

import Foundation

// A Playground shows the value of each line in the results margin.
let greeting = "Hello"
let name = "iOS"

// String interpolation: anything inside \( ) is evaluated and inserted.
let message = "\(greeting), \(name)!"
print(message)

// The margin shows 5, then 25 - the result of each expression.
let numbers = [1, 2, 3, 4, 5]
let total = numbers.reduce(0, +)
let squared = numbers.map { $0 * $0 }

print("count: \(numbers.count), total: \(total)")
print("squared: \(squared)")