Introduction to Swift

Reviewed & published by Brayan K

Swift is Apple's modern, fast, and safe programming language for building apps across iOS, macOS, watchOS, and tvOS, introduced in 2014 to replace Objective-C.

Part of the free Swift course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

By the end of this lesson you'll know what Swift is and where it runs, you'll understand print() and comments, and you'll have written and run your very first Swift program.

What You'll Learn in This Lesson

1️⃣ What is Swift (and where does it run)?

Swift is Apple's modern, fast, and safe programming language. Apple introduced it in 2014 to replace the older Objective-C, and it's now how almost every new iPhone, iPad, Mac, Apple Watch, and Apple TV app is built. "Safe" means the language is designed to catch whole classes of mistakes before your program runs, so your apps crash far less often.

So where do you actually write Swift? You have three common homes for it: Xcode (Apple's free, full code editor on the Mac, used to build real apps), Swift Playgrounds (a friendly app on Mac and iPad for learning and experimenting), and free online editors such as swiftfiddle.com or Replit that run in any browser. For this course, an online editor is the quickest way to start — you don't need a Mac yet.

2️⃣ Your first program: print() and comments

Every Swift journey starts with output. The print() function takes whatever you put between its parentheses and shows it in the console. Text goes inside "double quotes"; numbers don't need quotes. Anything after // is a comment — a note for humans that Swift ignores. Read this worked example, then run it and confirm the output matches.

// This is a comment — Swift ignores everything after the //.
// Comments are notes for humans; they never run.

/* You can also write a comment that
   spans several lines like this. */

// print(...) shows a line of text in the console (the output panel).
print("Hello, World!")          // shows: Hello, World!
print("I'm learning Swift!")    // shows: I'm learning Swift!

// You can print numbers too — no quotes needed for numbers.
print(2 + 2)                    // shows: 4

Now a slightly bigger first program. A let creates a constant — a named value. The real magic is string interpolation: write \(name) inside a string and Swift swaps in the value of name. It reads far better than sticking pieces of text together by hand.

// A first real program: greet a person by name.

// 'let' makes a constant — a named value that never changes.
let name = "Sam"
let year = 2014

// \(...) is "string interpolation": Swift drops the value into the text.
print("Hi \(name)! Welcome to Swift.")     // Hi Sam! Welcome to Swift.
print("Swift launched in \(year).")        // Swift launched in 2014.

// print can join values with commas — note the automatic spaces.
print("Two", "plus", "two", "is", 2 + 2)   // Two plus two is 4

3️⃣ Your turn: print and comment

Time to write some code. The program below is almost finished — just fill in the two blanks marked ___ using the // 👉 hints, then run it and check your output against the expected lines.

// 🎯 YOUR TURN — replace each ___ then run it (try SwiftFiddle, link below).

// 1) Print a friendly greeting of your choice
print(___)                 // 👉 put your text inside "double quotes"

// 2) Print the answer to 10 + 5  (numbers need no quotes)
print(___)                 // 👉 replace ___ with  10 + 5

// 3) Write a // comment above the next line saying what it does
print("Comments are free — use them!")

// ✅ Expected output (example):
//    Hello there!
//    15
//    Comments are free — use them!

4️⃣ Your turn: interpolate your own details

One more guided exercise, this time with string interpolation. Fill in the name and age blanks, then run it — the print line already works once your constants exist.

// 🎯 YOUR TURN — fill in the blanks to greet YOU.

// 1) Make a constant 'name' holding your name (text in "double quotes")
let name = ___             // 👉 e.g. "Alex"

// 2) Make a constant 'age' holding your age (a whole number, no quotes)
let age = ___              // 👉 e.g. 21

// This line already works once your constants exist.
// \(name) and \(age) get replaced by their values.
print("My name is \(name) and I am \(age).")

// ✅ Expected output (example):
//    My name is Alex and I am 21.

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — Swift Basics

ConceptSwift Syntax
Show outputprint("Hello, World!")
Single-line comment// a note for humans
Multi-line comment/* spans lines */
Constant (never changes)let name = "Alice"
String interpolationprint("Hi \(name)!")
Print a numberprint(2 + 2)

Mini-Challenge: About-Me Card

No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it on SwiftFiddle, and check your output against the example in the comments.

// 🎯 MINI-CHALLENGE: About-Me card
// 1. Make three constants with 'let':
//      name (your name), city (where you live), and favourite (a hobby).
// 2. Print three lines using string interpolation \(...) :
//      "Name: <name>", "City: <city>", "I love <favourite>."
// 3. BONUS: add a // comment above each print saying what it shows.
//
// ✅ Example output:
//    Name: Sam
//    City: Leeds
//    I love coding.

// your code here

🎉 Lesson Complete!

Practice quiz

Which function shows a line of text in the Swift console?

  • print("Hi")
  • echo("Hi")
  • log("Hi")
  • write("Hi")

Answer: print("Hi"). print() writes a line to the console.

How do you start a single-line comment in Swift?

  • #
  • //
  • --
  • /*

Answer: //. // begins a single-line comment; Swift ignores the rest of the line.

Which keyword declares a constant in Swift?

  • const
  • var
  • let
  • final

Answer: let. let makes a constant whose value never changes.

How do you drop a value into a string (interpolation)?

  • ${name}
  • {name}
  • %name%
  • \(name)

Answer: \(name). Swift interpolation uses a backslash and parentheses: \(name).

In which year did Apple introduce Swift?

  • 2014
  • 2010
  • 2008
  • 2018

Answer: 2014. Swift was introduced in 2014 to replace Objective-C.

Does Swift require a semicolon at the end of each line?

  • Yes, always
  • No, they are optional
  • Only inside functions
  • Only for print

Answer: No, they are optional. Swift treats a line end as a statement end, so semicolons are optional.

Which is the correct multi-line comment syntax?

  • ### ... ###
  • <!-- ... -->
  • /* ... */
  • --[[ ... ]]

Answer: /* ... */. Swift wraps multi-line comments in /* and */.

Do you need a Mac to start learning Swift?

  • Yes, always
  • Yes, plus an iPhone
  • Only for print()
  • No, free online editors run it in a browser

Answer: No, free online editors run it in a browser. Tools like SwiftFiddle run Swift in the browser; a Mac is only needed to ship apps.

Text in Swift must be wrapped in...

  • Double quotes
  • Single quotes
  • Backticks
  • Angle brackets

Answer: Double quotes. Swift string literals use "double quotes".

What does Swift's 'safe' design goal mean?

  • It encrypts your code
  • It catches whole classes of mistakes before the program runs
  • It runs only on Apple devices
  • It hides your source

Answer: It runs only on Apple devices. Swift catches many errors at compile time, so apps crash less often.

Continue this course

Frequently asked questions

Do I need a Mac to learn Swift?

No. You can write and run Swift entirely in your browser using a free site like SwiftFiddle or Replit — no installation and no Apple device required. You only need macOS and Xcode later, when you want to build and ship actual iOS or macOS apps.

What is print() actually doing?

print() writes a line of text to the console — the output area where your program's results appear. It is the simplest way to see what your code is doing, and you will use it constantly while learning to check that values are what you expect.

What does \(name) mean inside a string?

That is string interpolation. Swift replaces \(name) with the current value of name. So if name is "Sam", then print("Hi \(name)") prints "Hi Sam". It is cleaner than gluing pieces of text together with the + sign.

Why does Swift not use semicolons at the end of lines?

Swift treats the end of a line as the end of a statement, so semicolons are optional. Leaving them off keeps code clean and readable. You only need a semicolon if you want to put two statements on the same line.