JSTAcademy
0 XP
Dashboard
Crash Courses
Xcode, Swift Playgrounds & Hello World
12 min
Basic+155 XP
Crash Courses · Basic

Xcode, Swift Playgrounds & Hello World

Install Xcode, explore Swift Playgrounds, understand the Swift compilation model, and write your first Swift program with proper formatting.
12 min read+155 XP on completionCert: Swift Crash Course
Tap any word in the text below to start reading from there.

Xcode, Swift Playgrounds & Hello World

What Makes Swift Different?

Swift was designed by Apple in 2014 to replace Objective-C. Its core design principles:

  1. Safety — optionals force you to handle null explicitly; strong typing catches errors at compile time
  2. Performance — compiles to native machine code; comparable to C in benchmarks
  3. Expressiveness — clean syntax, type inference, value types, protocol-oriented design
  4. Interoperability — works side-by-side with Objective-C in the same project

Swift runs on macOS, iOS, iPadOS, watchOS, tvOS, and on Linux (server-side via Swift on the server). It is the primary language for all Apple platform development.

---

Installing Xcode

Xcode includes the Swift compiler, iOS/macOS simulators, and all Apple SDKs.

Mac (required for iOS/macOS development):

  1. Open the App Store
  2. Search "Xcode" — download Xcode 15 or later (it is ~13GB)
  3. Open it once to agree to the license and install components
  4. Verify: open Terminal, run: swift --version

Linux (no Xcode):

# Download Swift toolchain from swift.org
wget https://download.swift.org/swift-5.10-release/ubuntu2204/swift-5.10-RELEASE/swift-5.10-RELEASE-ubuntu22.04.tar.gz
tar xzf swift-5.10-RELEASE-ubuntu22.04.tar.gz
export PATH="$PWD/swift-5.10-RELEASE-ubuntu22.04/usr/bin:$PATH"
swift --version

Windows: Use Swift on Windows (swift.org/install) or run code in an online Swift environment like swiftfiddle.com for this crash course.

---

Swift Playgrounds

The fastest way to learn Swift — code runs as you type:

  1. Open Xcode → File → New → Playground
  2. Select "Blank" → macOS or iOS → Create
  3. Code executes when you click the play button (or Shift+Enter for a line)
  4. Results appear in the sidebar; use print() for console output

Alternatively: run Swift scripts from Terminal:

# Create a file
echo 'print("Hello, Swift!")' > hello.swift
# Run it directly (no compile step needed)
swift hello.swift

---

Hello World and Swift Basics

// Single-line comment
/* Multi-line
   comment */

// print() outputs to console. No semicolons required.
print("Hello, World!")
print("Hello, Swift 5.10!")

// String interpolation — embed expressions with \(...)
let name = "Jordan"
let year = 2025
print("Hello, \(name)! Year: \(year)")
print("2 + 2 = \(2 + 2)")  // arithmetic inside interpolation

// Multi-line strings
let message = """
    Swift is:
    - Safe
    - Fast
    - Expressive
    """
print(message)

---

Swift's Type System

Swift is statically typed — every variable has a type known at compile time. But you rarely write the type explicitly because type inference handles it:

let greeting = "Hello"     // inferred as String
let count    = 42          // inferred as Int
let pi       = 3.14159     // inferred as Double
let isActive = true        // inferred as Bool

// Explicit types (use when the inferred type would be wrong):
let bigNumber: Int64   = 9_223_372_036_854_775_807
let smallFloat: Float  = 3.14  // Float (32-bit), not Double (64-bit)
let ascii: Character   = "A"

// Type annotation required when no initial value:
var currentUser: String  // no value yet — must annotate
currentUser = "Alice"

Swift is strict about types. Unlike JavaScript, "5" + 5 is a compile error. You must convert explicitly:

let numStr = "42"
let num    = Int(numStr) ?? 0  // Int("42") returns Int? (optional) — ?? provides default
print(num + 8)  // 50

---

Your First Command-Line Program: Unit Converter

func kilometersToMiles(_ km: Double) -> Double {
    return km * 0.621371
}

func milesToKilometers(_ miles: Double) -> Double {
    return miles / 0.621371
}

func celsiusToFahrenheit(_ c: Double) -> Double {
    return c * 9.0 / 5.0 + 32.0
}

let distances: [Double] = [1.0, 5.0, 10.0, 42.195]  // km

print("Distance Conversions:")
for km in distances {
    let miles = kilometersToMiles(km)
    print(String(format: "  %.3f km = %.3f miles", km, miles))
}

print("\nTemperature Conversions:")
let temps = [0.0, 20.0, 37.0, 100.0]  // Celsius
for c in temps {
    print(String(format: "  %.1f°C = %.1f°F", c, celsiusToFahrenheit(c)))
}

Swift function parameters use argument labels (_ km: means the caller doesn't write the label: kilometersToMiles(5.0)). We cover this in Module 3.

---

Swift Package Manager (SPM) for CLI Programs

For real projects beyond Playgrounds:

mkdir swift-crash-course
cd swift-crash-course
swift package init --type executable

# Project structure:
# Package.swift          — project manifest
# Sources/swift-crash-course/main.swift — entry point

# Run:
swift run
# Build only:
swift build

Edit Sources/swift-crash-course/main.swift and swift run to execute.

0%