Swift in VSCode: Write the first hello world program
Video: Swift in VSCode: Write the first hello world program by Taught by Celeste AI - AI Coding Coach
Swift with Copilot: Hello World in VSCode
First Swift program in VS Code. Install Swift, install the VS Code Swift extension, write
print("Hello, World!"), run withswift hello.swift. Copilot autocompletes from a comment — but for one line, you barely need it.
This is the orientation episode for the series. Get Swift running outside Xcode, see Copilot fire off a suggestion, and confirm everything works.
Setup: Swift on macOS
# Swift ships with Xcode. If you have Xcode installed:
swift --version
# Apple Swift version 5.10 ...
# If not, install command-line tools (~3 GB):
xcode-select --install
For Linux, download from swift.org.
VS Code setup
Install the Swift extension (by Swift Server Work Group). It provides:
- Syntax highlighting.
- LSP integration via
sourcekit-lsp. - "Run Swift File" command.
- Debugging support.
Open VS Code, install the Swift extension, install the GitHub Copilot extension, and sign in.
The Copilot prompt
// Print "Hello, World!" to the console
Copilot completes:
// Print "Hello, World!" to the console
print("Hello, World!")
Save as hello.swift. Run:
swift hello.swift
# Hello, World!
swift script.swift runs a Swift file directly — no compilation step needed for one-off scripts. Internally, Swift compiles to a binary in a temp dir, runs it, deletes it.
What the line does
print("Hello, World!")
print(_:)is Swift's standard output function.- The argument is a string literal — Swift uses double quotes only.
- Default appends a newline; pass
terminator: ""to suppress.
print("No newline", terminator: "")
print(" — same line")
Multiple arguments
print("A", "B", "C")
// A B C — separated by spaces by default
print("A", "B", "C", separator: "-")
// A-B-C
separator: and terminator: are the two named parameters.
Building a binary
For real apps, you'd use Swift Package Manager:
mkdir hello && cd hello
swift package init --type executable
swift run
# Hello, world!
swift package init --type executable scaffolds a Package.swift, Sources/hello/main.swift, etc. swift run builds and runs.
For now, single-file .swift scripts are fine.
Why VS Code instead of Xcode?
- Speed. VS Code starts in 1 second; Xcode in 10+.
- Familiar if you write JS/Python/Go in VS Code already.
- Copilot integrates everywhere.
- Cross-platform — same setup on Linux.
For iOS/macOS app development, you still need Xcode (Storyboards, SwiftUI previews, Simulator, signing). For Swift learning and server-side Swift, VS Code is fine.
Other ways to run Swift
# REPL
swift
> print("Hello")
> :quit
# Inline expression
swift -e 'print(1 + 2)' # 3
# Swift script with arguments
swift hello.swift arg1 arg2
# As a shebang script
cat > hello.sh <<'EOF'
#!/usr/bin/env swift
print("Hello, World!")
EOF
chmod +x hello.sh
./hello.sh
#!/usr/bin/env swift makes a Swift file executable. Slow startup, but useful for quick scripts.
Common stumbles
swift: command not found. Xcode CLI tools missing. xcode-select --install.
LSP not working in VS Code. Open command palette → "Swift: Restart Language Server."
Print without newline using \n? print(...) already adds a newline; \n inside the string adds an extra. Pass terminator: "" to suppress the auto newline.
Single quotes. Swift requires double quotes for strings. 'hello' errors.
println from Swift 1.x. Removed. Just print.
What's next
Episode 2: Create a list of 5 random numbers. Arrays, Int.random(in:), map.
Recap
swift file.swift runs a single file. print("...") for output; separator: and terminator: named args. VS Code + Swift extension + Copilot is the modern setup outside Xcode. Use Swift Package Manager (swift package init) for real projects. Make scripts executable with #!/usr/bin/env swift + chmod +x.
Next episode: a list of 5 random numbers.