Introduction
Before writing Swift code, you need a suitable development environment. Apple provides several tools: Xcode, the full IDE that includes the Swift compiler and Interface Builder; Swift Playgrounds, for quick interactive experimentation; and the 'swift' command-line REPL for trying out code snippets. For managing dependencies in larger projects, Swift Package Manager (SPM) is the standard tool. Swift can also run on Linux, which is important for server-side development.
Cricket analogy: Before playing, you need the right kit — Xcode is like a full training academy with coaches and nets built in, Swift Playgrounds is like a backyard net for quick practice, the REPL is a quick shadow-batting session, SPM manages your equipment suppliers, and Linux support means you can practice even off the main ground.
Syntax
// Launching the Swift REPL from a terminal
// $ swift
// Welcome to Swift!
// 1> print("Hello from the REPL")Explanation
Running 'swift' with no arguments in a terminal starts the Read-Eval-Print Loop (REPL), where you can type Swift expressions and see results immediately, which is useful for quick experiments. On macOS, Xcode bundles this same Swift compiler along with a graphical IDE and Interface Builder for designing user interfaces. On Linux, installing the official Swift toolchain gives you the compiler and 'swift' command-line tools needed for server-side development without Xcode.
Cricket analogy: Running the REPL is like a quick net session where you see the result of every shot immediately; Xcode on macOS is like a full stadium bundling nets, scoreboard, and coaching staff together, while installing the toolchain on Linux is like practicing at a regional ground with just the essentials, no stadium extras.
Example
// Package.swift for a project using Swift Package Manager
// swift-tools-version:5.9
import PackageDescription
let package = Package(
name: "HelloSwift",
targets: [
.executableTarget(name: "HelloSwift")
]
)Output
Running 'swift build' in a directory containing this Package.swift file compiles the 'HelloSwift' target, and 'swift run' then executes the resulting binary, printing any output defined in the target's source files to the terminal.
Cricket analogy: Running 'swift build' is like a training session that assembles all your drills into match-ready form, compiling the 'HelloSwift' squad from the Package.swift roster sheet, and 'swift run' is like walking out to bat, executing the plan and producing the scoreboard output for everyone to see.
Key Takeaways
- Xcode is Apple's IDE, bundling the Swift compiler and Interface Builder.
- Swift Playgrounds offers a lightweight, interactive way to experiment with Swift code.
- The 'swift' command-line REPL lets you evaluate Swift expressions interactively.
- Swift Package Manager (SPM) manages dependencies and project structure via Package.swift.
- The Swift toolchain can be installed on Linux for server-side Swift development.
Practice what you learned
1. What is Xcode?
2. What is the purpose of the 'swift' command-line REPL?
3. What file defines a project's structure and dependencies when using Swift Package Manager?
4. Can Swift be developed and run on Linux?
5. What is Swift Playgrounds primarily used for?
Was this page helpful?
You May Also Like
Introduction to Swift Programming
Swift is Apple's modern, safe, and fast programming language for building apps across its platforms and beyond.
History and Evolution of Swift
Swift evolved from a 2014 Apple announcement into an open-source, ABI-stable language used across and beyond Apple platforms.
Features of Swift
Swift combines safety, speed, and modern syntax through optionals, type inference, ARC, and a powerful LLVM-based compiler.
Concurrency in Swift (async/await)
Explore Swift's modern async/await concurrency model, Task, and actors for writing safe asynchronous code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics