Swift Introduction

Learn what Swift is used for, where to run Swift code, and write your first program with print and comments.

Lesson 1 of 43Beginner18 min read

Swift is the programming language Apple created in 2014 for building apps on iPhone, iPad, Mac, Apple Watch, Apple TV and Vision Pro. It replaced the older Objective-C as Apple's main language, and today almost every new app in the App Store is written in Swift. Swift is also open source, so it runs on Linux and Windows and is used for servers and command-line tools.

Swift was designed to be safe, fast and pleasant to read. The compiler catches many mistakes before your code runs: values that might be missing must be handled explicitly, types can't be mixed up by accident, and variables must have a value before they're used. At the same time, the syntax is clean enough that short programs read almost like English.

In this lesson you'll learn where to write Swift, print your first lines of text and add comments. No previous programming experience is needed.

Where to write and run Swift

  • Swift Playgrounds (iPad and Mac): Apple's friendly app for learning, with interactive lessons and live results.
  • Xcode (Mac): Apple's full development environment, used to build real apps. Choose File, New, Playground to experiment, or create an app project.
  • Windows and Linux: install Swift from swift.org, then run files from a terminal with swift main.swift, or use VS Code with the Swift extension.
  • Online: sites such as the SwiftFiddle playground run Swift in a browser with nothing to install.

Swift source files end in .swift. Every example in this course can run in any of these.

More about print, and comments

print can take several values separated by commas, and puts a space between them. Its separator and terminator options change what goes between values and at the end:

Swift
print("A", "B", "C")                   // A B C
print("A", "B", separator: "-")        // A-B
print("Loading", terminator: "")       // no new line afterwards

Comments are notes for people that Swift ignores. // starts a comment that runs to the end of the line, and /* ... */ can span several lines. Use them to explain why code does something, rather than repeating what it obviously does.

Running a script versus compiling a program

Swift is a compiled language: before your code runs, the compiler checks the whole file and turns it into machine code your computer can execute directly. That's why typing mistakes are reported before a single line runs, and why Swift programs are fast.

From a terminal you can work in two ways:

Terminal
swift hello.swift              # compile in memory and run straight away
swiftc hello.swift -o hello    # build a program file called hello
./hello                        # run the program you built

The first form is handy while learning, because one command does everything. The second produces a standalone program you can run again without compiling, or give to someone with the same kind of computer. Xcode and Swift Playgrounds do the same job for you behind a Run button.

Swift Basics Syntax

A Swift program

// a one-line comment
print("Text on its own line")
print("A", "B", separator: ", ")
print("No new line", terminator: "")

Swift Basics Summary

  • Swift is Apple's language for iPhone, iPad, Mac and Watch apps, and also runs on Linux and Windows.
  • Use Swift Playgrounds or Xcode on Apple devices, or install Swift from swift.org elsewhere.
  • print shows values; separator and terminator change spacing and line endings.
  • Top-level code in a playground or script runs from top to bottom; semicolons are optional.
  • // and /* */ write comments that Swift ignores.

Swift Basics Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Your first program

Swift
print("Hello, Swift!")
print("Welcome to Codevra.")
Output
Hello, Swift!
Welcome to Codevra.
Line-by-line explanation
  1. Lines 1 to 2Each print shows its text on its own line.
Example 2

print with several values

Swift
print("Swift", "is", "fun")
print(2024, "is the year", separator: " - ")
print("Loading", terminator: "")
print("...")
print("Done!")
Output
Swift is fun
2024 - is the year
Loading...
Done!
Line-by-line explanation
  1. Line 1Several values are joined with a space by default.
  2. Line 2separator changes what goes between the values; numbers can be printed too.
  3. Lines 3 to 4terminator: "" stops print adding a new line, so the next print continues the same line.
Example 3

Calculations and comments

Swift
// Swift can calculate as well as print text
print(7 + 5)
print("7 + 5")
/* The first line prints the result of the sum.
   The second prints the characters exactly as written. */
print(10 * 3)
Output
12
7 + 5
30
Line-by-line explanation
  1. Line 2Without quotes, Swift calculates the sum.
  2. Line 3Inside quotes it's just text.
  3. Lines 4 to 5A multi-line comment, ignored when the code runs.

Common Swift Basics Mistakes

Capital P in print

Don't
Print("Hi")          // error: cannot find 'Print' in scope
Do
print("Hi")

Single quotes for text

Don't
print('Hello')       // error: single-quoted string literal found
Do
print("Hello")

Swift strings always use double quotes.

Forgetting a closing quote or bracket

Don't
print("Hello)
Do
print("Hello")

Swift Basics in Real Projects

Swift in practice

Apps such as Airbnb, LinkedIn, Lyft and many of Apple's own apps are written largely in Swift. Swift also runs the server side of some companies' services through frameworks like Vapor, and it's used for command-line tools and scripts on Mac and Linux. Learning Swift is the first step towards publishing your own app on the App Store.

Swift Basics FAQ

Is Swift hard to learn for a complete beginner?

Swift is one of the friendlier first languages. The syntax is short, print("Hello") is a complete program, and the compiler explains mistakes before code runs. The harder parts, such as optionals and concurrency, come later and build on the basics step by step.

Do I need a Mac to learn Swift?

Not to learn the language. Swift runs on Linux and Windows from swift.org, and online playgrounds run it in a browser. You do need a Mac with Xcode to build and publish iPhone or Mac apps, and SwiftUI only works on Apple platforms.

What is the difference between Swift and Objective-C?

Objective-C is the older language Apple inherited from NeXT and used for Mac and iPhone apps until Swift arrived in 2014. Swift has cleaner syntax, safer handling of missing values and stricter types. Both can live in the same app, so older projects often mix them, but new Apple code is written in Swift.

What is the difference between Swift and SwiftUI?

Swift is the programming language. SwiftUI is Apple's framework, written for Swift, for building user interfaces on iPhone, iPad, Mac, Watch and TV. You learn Swift first, then use it to describe screens with SwiftUI.

Why does Swift not need semicolons?

Swift treats the end of a line as the end of a statement, so semicolons are optional. You only need one to put two statements on the same line, such as let a = 1; let b = 2, and most style guides avoid even that.

Swift Basics Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the line so it shows Hi in the console.

("Hi")
Predict the outputeasy

What does this print?

print("A", "B", "C", separator: "")
Predict the outputmedium

What does this print?

print("2 + 2")
print(2 + 2)
Multiple choicemedium

Which file extension do Swift source files use?

Multiple choicehard

Which tool is used to build and publish iPhone apps?

Swift Basics Quiz

5 questions to check what you learned.

  1. Which company created Swift?

    • Apple
    • Google
    • Microsoft
    • JetBrains
  2. Do Swift programs need a main function in a playground?

    • Yes, always
    • No, top-level code runs from top to bottom
    • Only for print
    • Only on Windows
  3. What does terminator: "" do in print?

    • Ends the program
    • Removes spaces
    • Stops print adding a new line at the end
    • Prints nothing
  4. Which quotes does Swift use for strings?

    • Single quotes
    • Backticks
    • Either single or double
    • Double quotes
  5. Can Swift run outside Apple devices?

    • Yes, Swift is open source and runs on Linux and Windows
    • No
    • Only in a browser
    • Only on Android