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
-
Open the Mac App Store
- Click the App Store icon in your Dock
- Or use Spotlight (⌘ + Space) and type “App Store”
-
Search for Xcode
- Type “Xcode” in the search bar
- Click on the Xcode app (blue icon with a hammer)
-
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
- Open Xcode from your Applications folder or Launchpad
- Accept the license agreement when prompted
- Install additional components if Xcode requests them
- Wait for Xcode to finish installing command line tools
Step 3: Verify Installation
- Open Terminal (in Applications → Utilities)
- Type the following command and press Enter:
swift --versionYou 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
- Download Swift from swift.org/download
- Extract the archive to a location like
C:\Library\Developer\Toolchains - Add Swift to PATH:
- Open System Properties → Environment Variables
- Add the Swift
bindirectory to your PATH
- Verify installation in Command Prompt:
swift --versionFor Linux Users (Ubuntu/Debian)
- Install dependencies:
sudo apt-get updatesudo apt-get install clang libicu-dev libpython3-dev- Download and install Swift:
wget https://download.swift.org/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gztar xzf swift-5.9-RELEASE-ubuntu22.04.tar.gzsudo mv swift-5.9-RELEASE-ubuntu22.04 /usr/share/swift- Add to PATH:
echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.bashrcsource ~/.bashrc- Verify installation:
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
- Create a new file called
hello.swift - Open it in your favorite text editor
- Type the following code:
// My first Swift programprint("Hello, World!")print("Welcome to Swift programming!")- Save the file
Running Your Program
Open Terminal (or Command Prompt on Windows) and navigate to the folder containing your file:
cd ~/Desktop # Or wherever you saved the fileswift hello.swiftOutput:
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
-
Open Xcode
-
Select “Get started with a playground” from the welcome screen
- Or go to File → New → Playground (⌥ + ⇧ + ⌘ + N)
-
Choose a template:
- Select “Blank” template
- Choose iOS as the platform
- Click Next
-
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 Constantsvar greeting = "Hello"let language = "Swift"
print("\(greeting), \(language)!")
// Basic arithmeticlet x = 10let y = 20let sum = x + yprint("Sum: \(sum)")
// Experimenting with stringsvar name = "Student"name = "Swift Developer" // We can change variablesprint("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
-
Open Xcode
-
Create a new project:
- Click “Create a new Xcode project”
- Or go to File → New → Project (⇧ + ⌘ + N)
-
Choose a template:
- Select iOS tab at the top
- Choose App template
- Click Next
-
Configure your project:
- Product Name: MyFirstApp
- Team: None (or your Apple Developer team)
- Organization Identifier: com.yourname
- Interface: SwiftUI
- Language: Swift
- Click Next
-
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 assetsRunning Your First App
- Select a simulator from the top toolbar (e.g., “iPhone 15 Pro”)
- Click the Play button (▶) or press ⌘ + R
- Wait for the simulator to launch
- 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:
swiftYou’ll see a prompt:
Welcome to Swift version 5.9Type :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 15Exiting 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:
- Stores your name in a variable
- Stores your age in a constant
- 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:
- Defines two numbers
- Calculates and prints their sum, difference, product, and quotient
Solution:
let num1 = 50let num2 = 10
let sum = num1 + num2let difference = num1 - num2let product = num1 * num2let quotient = num1 / num2
print("Sum: \(sum)")print("Difference: \(difference)")print("Product: \(product)")print("Quotient: \(quotient)")Output:
Sum: 60Difference: 40Product: 500Quotient: 5Exercise 3: Temperature Converter
Create a program that converts Celsius to Fahrenheit:
Formula: °F = (°C × 9/5) + 32
Solution:
let celsius = 25.0let 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
| Shortcut | Action |
|---|---|
| ⌘ + R | Run your project |
| ⌘ + B | Build your project |
| ⌘ + . | Stop running |
| ⌘ + / | Comment/uncomment lines |
| ⌘ + [ | Indent left |
| ⌘ + ] | Indent right |
| ⌃ + I | Auto-indent selection |
| ⌘ + F | Find in file |
| ⌘ + ⇧ + F | Find 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
- Website: online-swift-playground.run
- Quick testing environment
- No installation required
[!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 (
varvslet) - 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:
xcode-select --installProblem: 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
- Official Swift Documentation: swift.org/documentation
- Swift.org Getting Started: swift.org/getting-started
- Apple’s Swift Book: docs.swift.org/swift-book
- Xcode Help: Inside Xcode, go to Help → Xcode Help
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!