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.
print and the structure of a program
print("Hello, Swift!")
print("I'm learning to code.")printshows text in the console and then moves to a new line.- In a playground or a script file, code at the top level runs from top to bottom, so there's no
mainfunction to write. - Semicolons at the end of lines are optional, and almost nobody uses them.
- Swift is case-sensitive:
printworks,Printdoesn't.
In a full app, code is organised into types and functions that the system calls, such as when a screen appears or a button is tapped, which you'll see in the SwiftUI lessons.
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:
print("A", "B", "C") // A B C
print("A", "B", separator: "-") // A-B
print("Loading", terminator: "") // no new line afterwardsComments 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:
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 builtThe 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.
print versus debugPrint
print is meant for people, so it shows text without its quotes. debugPrint shows values the way you'd write them in code, which helps when you're hunting a bug and need to see exactly what a value contains:
print("Hi") // Hi
debugPrint("Hi") // "Hi"
debugPrint(3, "a") // 3 "a"With debugPrint you can spot a stray space at the end of a string, or tell the number 3 apart from the text "3". In finished apps, console output isn't visible to users at all, so treat both as tools for you, the developer.
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.
Your first program
print("Hello, Swift!")
print("Welcome to Codevra.")Hello, Swift!
Welcome to Codevra.Line-by-line explanation
- Lines 1 to 2Each print shows its text on its own line.
print with several values
print("Swift", "is", "fun")
print(2024, "is the year", separator: " - ")
print("Loading", terminator: "")
print("...")
print("Done!")Swift is fun
2024 - is the year
Loading...
Done!Line-by-line explanation
- Line 1Several values are joined with a space by default.
- Line 2separator changes what goes between the values; numbers can be printed too.
- Lines 3 to 4terminator: "" stops print adding a new line, so the next print continues the same line.
Calculations and comments
// 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)12
7 + 5
30Line-by-line explanation
- Line 2Without quotes, Swift calculates the sum.
- Line 3Inside quotes it's just text.
- Lines 4 to 5A multi-line comment, ignored when the code runs.
Common Swift Basics Mistakes
Capital P in print
Print("Hi") // error: cannot find 'Print' in scopeprint("Hi")Single quotes for text
print('Hello') // error: single-quoted string literal foundprint("Hello")Swift strings always use double quotes.
Forgetting a closing quote or bracket
print("Hello)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.
Complete the line so it shows Hi in the console.
("Hi")
What does this print?
print("A", "B", "C", separator: "")
What does this print?
print("2 + 2")
print(2 + 2)
Which file extension do Swift source files use?
Which tool is used to build and publish iPhone apps?
Swift Basics Quiz
5 questions to check what you learned.
Which company created Swift?
- Apple
- Microsoft
- JetBrains
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
What does
terminator: ""do in print?- Ends the program
- Removes spaces
- Stops print adding a new line at the end
- Prints nothing
Which quotes does Swift use for strings?
- Single quotes
- Backticks
- Either single or double
- Double quotes
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