Skip to content

Getting Started with Swift

Welcome to your first hands-on Swift lesson! In this guide, you’ll learn how to set up your development environment, create your first Swift project, and start writing code. By the end of this tutorial, you’ll have everything you need to begin your Swift programming journey.

📋 Prerequisites

Before we begin, make sure you have:

  • A Mac computer running macOS 12.0 or later (for Xcode)
  • OR Windows/Linux computer (for Swift command-line tools)
  • At least 10GB of free disk space (for Xcode installation)
  • Basic computer skills and enthusiasm to learn!

[!NOTE] While Swift runs on multiple platforms, this guide focuses primarily on macOS with Xcode, as it provides the best Swift development experience, especially for iOS development.

🛠️ Setting Up Your Development Environment

Option 1: Installing Xcode (macOS Users)

Xcode is Apple’s integrated development environment (IDE) that includes everything you need to develop Swift applications.

Step 1: Download Xcode

  1. Open the Mac App Store

    • Click the App Store icon in your Dock
    • Or use Spotlight (⌘ + Space) and type “App Store”
  2. Search for Xcode

    • Type “Xcode” in the search bar
    • Click on the Xcode app (blue icon with a hammer)
  3. Download and Install

    • Click the “Get” or “Download” button
    • Enter your Apple ID credentials if prompted
    • Wait for the download to complete (it’s about 7-13GB)

[!TIP] The download can take 30 minutes to several hours depending on your internet speed. You can continue other work while it downloads.

Step 2: Launch Xcode

  1. Open Xcode from your Applications folder or Launchpad
  2. Accept the license agreement when prompted
  3. Install additional components if Xcode requests them
  4. Wait for Xcode to finish installing command line tools

Step 3: Verify Installation

  1. Open Terminal (in Applications → Utilities)
  2. Type the following command and press Enter:
Terminal window
swift --version

You should see output similar to:

swift-driver version: 1.87.3 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Target: arm64-apple-macosx14.0

Success! You now have Swift installed and ready to use.

Option 2: Installing Swift on Windows/Linux

For Windows Users

  1. Download Swift from swift.org/download
  2. Extract the archive to a location like C:\Library\Developer\Toolchains
  3. Add Swift to PATH:
    • Open System Properties → Environment Variables
    • Add the Swift bin directory to your PATH
  4. Verify installation in Command Prompt:
Terminal window
swift --version

For Linux Users (Ubuntu/Debian)

  1. Install dependencies:
Terminal window
sudo apt-get update
sudo apt-get install clang libicu-dev libpython3-dev
  1. Download and install Swift:
Terminal window
wget https://download.swift.org/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gz
tar xzf swift-5.9-RELEASE-ubuntu22.04.tar.gz
sudo mv swift-5.9-RELEASE-ubuntu22.04 /usr/share/swift
  1. Add to PATH:
Terminal window
echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
  1. Verify installation:
Terminal window
swift --version

🎮 Your First Swift Program - The Traditional Way

Let’s write your first Swift program using a simple text file and the command line.

Creating a Swift File

  1. Create a new file called hello.swift
  2. Open it in your favorite text editor
  3. Type the following code:
// My first Swift program
print("Hello, World!")
print("Welcome to Swift programming!")
  1. Save the file

Running Your Program

Open Terminal (or Command Prompt on Windows) and navigate to the folder containing your file:

Terminal window
cd ~/Desktop # Or wherever you saved the file
swift hello.swift

Output:

Hello, World!
Welcome to Swift programming!

🎉 Congratulations! You just wrote and ran your first Swift program!

Understanding the Code

// My first Swift program
  • This is a comment - it’s ignored by Swift
  • Comments help you document your code
  • Use // for single-line comments
print("Hello, World!")
  • print() is a function that displays output
  • The text inside quotes is a String
  • No semicolon needed! (they’re optional in Swift)

🎪 Using Swift Playgrounds

Swift Playgrounds is an interactive environment perfect for learning Swift. You can write code and see results immediately!

Creating a Playground in Xcode

  1. Open Xcode

  2. Select “Get started with a playground” from the welcome screen

    • Or go to File → New → Playground (⌥ + ⇧ + ⌘ + N)
  3. Choose a template:

    • Select “Blank” template
    • Choose iOS as the platform
    • Click Next
  4. Name your playground:

    • Name it “MyFirstPlayground”
    • Choose where to save it
    • Click Create

Your First Playground Code

You’ll see a playground with some default code. Replace it with this:

import Foundation
// Variables and Constants
var greeting = "Hello"
let language = "Swift"
print("\(greeting), \(language)!")
// Basic arithmetic
let x = 10
let y = 20
let sum = x + y
print("Sum: \(sum)")
// Experimenting with strings
var name = "Student"
name = "Swift Developer" // We can change variables
print("Welcome, \(name)!")

How Playgrounds Work

  • Instant Feedback: Results appear on the right side as you type
  • Interactive: Modify code and see changes immediately
  • Visual: Hover over result bubbles to see values
  • Perfect for Learning: No need to compile and run each time

[!TIP] Press ⌘ + Return to manually re-run your playground code if automatic execution is disabled.

📱 Creating Your First Xcode Project

For more complex programs and apps, you’ll use Xcode projects instead of playgrounds.

Step-by-Step Project Creation

  1. Open Xcode

  2. Create a new project:

    • Click “Create a new Xcode project”
    • Or go to File → New → Project (⇧ + ⌘ + N)
  3. Choose a template:

    • Select iOS tab at the top
    • Choose App template
    • Click Next
  4. Configure your project:

    • Product Name: MyFirstApp
    • Team: None (or your Apple Developer team)
    • Organization Identifier: com.yourname
    • Interface: SwiftUI
    • Language: Swift
    • Click Next
  5. Choose a location and click Create

Understanding the Project Structure

Your new project contains several files:

MyFirstApp/
├── MyFirstAppApp.swift # App entry point
├── ContentView.swift # Main view
├── Assets.xcassets # Images and colors
└── Preview Content/ # Preview assets

Running Your First App

  1. Select a simulator from the top toolbar (e.g., “iPhone 15 Pro”)
  2. Click the Play button (▶) or press ⌘ + R
  3. Wait for the simulator to launch
  4. See your app running!

You should see a simple app displaying “Hello, world!”

Modifying the Default App

Open ContentView.swift and you’ll see:

import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
#Preview {
ContentView()
}

Let’s customize it:

import SwiftUI
struct ContentView: View {
var body: some View {
VStack(spacing: 20) {
Image(systemName: "swift")
.imageScale(.large)
.foregroundStyle(.orange)
.font(.system(size: 60))
Text("My First Swift App!")
.font(.title)
.fontWeight(.bold)
Text("I'm learning Swift programming")
.font(.subheadline)
.foregroundColor(.gray)
Button(action: {
print("Button tapped!")
}) {
Text("Click Me")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
}
.padding()
}
}
#Preview {
ContentView()
}

Press ⌘ + R to run again and see your changes!

💻 Using the Swift REPL (Read-Eval-Print Loop)

The Swift REPL is an interactive command-line tool for experimenting with Swift code.

Starting the REPL

Open Terminal and type:

Terminal window
swift

You’ll see a prompt:

Welcome to Swift version 5.9
Type :help for assistance.
1>

Interactive Coding

Try these commands:

1> let message = "Hello from REPL"
message: String = "Hello from REPL"
2> print(message)
Hello from REPL
3> let numbers = [1, 2, 3, 4, 5]
numbers: [Int] = 5 values {
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
}
4> let sum = numbers.reduce(0, +)
sum: Int = 15
5> print("Sum is \(sum)")
Sum is 15

Exiting the REPL

Type :quit or press Ctrl + D

🎯 Practice Exercises

Now that you have your environment set up, try these exercises:

Exercise 1: Personalized Greeting

Create a playground and write code that:

  1. Stores your name in a variable
  2. Stores your age in a constant
  3. Prints a greeting with your name and age

Solution:

var myName = "John"
let myAge = 25
print("Hello! My name is \(myName) and I am \(myAge) years old.")

Exercise 2: Simple Calculator

Write a program that:

  1. Defines two numbers
  2. Calculates and prints their sum, difference, product, and quotient

Solution:

let num1 = 50
let num2 = 10
let sum = num1 + num2
let difference = num1 - num2
let product = num1 * num2
let quotient = num1 / num2
print("Sum: \(sum)")
print("Difference: \(difference)")
print("Product: \(product)")
print("Quotient: \(quotient)")

Output:

Sum: 60
Difference: 40
Product: 500
Quotient: 5

Exercise 3: Temperature Converter

Create a program that converts Celsius to Fahrenheit:

Formula: °F = (°C × 9/5) + 32

Solution:

let celsius = 25.0
let fahrenheit = (celsius * 9/5) + 32
print("\(celsius)°C is equal to \(fahrenheit)°F")

Output:

25.0°C is equal to 77.0°F

🔧 Essential Xcode Tips for Beginners

Keyboard Shortcuts

ShortcutAction
⌘ + RRun your project
⌘ + BBuild your project
⌘ + .Stop running
⌘ + /Comment/uncomment lines
⌘ + [Indent left
⌘ + ]Indent right
⌃ + IAuto-indent selection
⌘ + FFind in file
⌘ + ⇧ + FFind in project

Auto-Completion

  • Start typing and press Escape to see suggestions
  • Use Tab to accept a suggestion
  • Xcode will help you write correct code!

Console Output

  • View print() output in the Debug Area (bottom of Xcode)
  • Show/hide with: View → Debug Area → Activate Console (⇧ + ⌘ + C)

Syntax Errors

  • Red errors mean code won’t compile
  • Yellow warnings are suggestions for improvement
  • Click on error messages for details and Fix-it suggestions

🌐 Online Swift Playgrounds

If you don’t have access to a Mac, try these online tools:

SwiftFiddle

  • Website: swiftfiddle.com
  • Write and run Swift code in your browser
  • Share code snippets with others
  • Supports multiple Swift versions

Online Swift Playground

[!WARNING] Online playgrounds are great for learning basics but won’t support iOS-specific frameworks or UI development.

📚 What You’ve Learned

Congratulations! You now know how to:

✅ Install Xcode and set up Swift on different platforms
✅ Write and run Swift programs from the command line
✅ Use Swift Playgrounds for interactive coding
✅ Create and run Xcode projects
✅ Use the Swift REPL for quick experimentation
✅ Navigate basic Xcode features and shortcuts
✅ Write simple Swift programs with output

🚀 Next Steps

Now that your environment is ready, you’re prepared to dive deeper into Swift fundamentals!

In the next lesson, you’ll learn about:

  • Variables and Constants (var vs let)
  • Type inference and type annotations
  • Naming conventions in Swift
  • When to use variables vs constants

💡 Common Beginner Issues

Problem: Xcode won’t install

Solution: Ensure you have enough disk space (at least 10GB) and are running a supported macOS version.

Problem: “Swift command not found”

Solution: Reinstall Xcode command line tools:

Terminal window
xcode-select --install

Problem: Simulator is slow

Solution: Close other apps, or choose an older/simpler simulator device (like iPhone SE).

Problem: Can’t see print() output

Solution: Open the Debug Area in Xcode (⇧ + ⌘ + Y) to see your console output.

🔗 Additional Resources


Ready to continue? Move on to the next lesson: Variables and Constants to start learning Swift’s core concepts! 🎯

Remember: The best way to learn programming is by doing. Type out all the examples yourself, experiment with changes, and don’t be afraid to make mistakes!