Swift lessons
Apple's iOS/macOS language
Swift is the language introduced by Apple in 2014 to replace Objective-C. The goal was to provide a language that is both safer and more readable: instead of Objective-C's bracketed call syntax and pointer management, a modern, clean writing style was introduced.
Today, iPhone, iPad, Mac, Apple Watch, and Apple TV apps are written in Swift. With the arrival of SwiftUI, interfaces are also defined declaratively within Swift; editing a separate interface file is no longer mandatory.
Swift's most prominent design decision is optional types. The possibility of a variable not holding a value is explicitly written in the type itself; therefore, crashes caused by accessing a null value are caught at compile time. For those with an Objective-C and Java background, this is the first topic to get used to.
- •→
Introduction to Swift
First step into Apple's iOS/macOS language Swift: printing with print, comments, and simple syntax.
- •→
Variables
Variables with var and constants with let; type annotation and value reassignment.
- •→
Data Types
Int, Double, String, Bool; string interpolation and text↔number conversion.
- •→
Operators
Math (+ - * / %), comparison (== != < >) and logical (&& || !) operators.
- •→
Strings
Concatenation, interpolation, length (count), and uppercase/lowercase conversion.
- •→
Arrays
Array creation, element access, adding (append), count, and iterating with loops.
- •→
Conditions (if / else)
Decision making with if-else if-else; conditions without parentheses, blocks with curly braces.
- •→
Loops (for / while)
for-in with ranges (1...5), iterating over arrays, and conditional repetition with while.
- •→
Functions
Definition with func, parameter labels, return, and default values.
What you will learn
- Printing output with print, and the distinction between let and var
- Type inference and explicit type declarations
- Using array, dictionary, and set structures
- Optional types, safe unwrapping, and the if let pattern
- Functions, closures, and parameter labels
- The difference between struct, class, and enum
- Organizing code with protocols and extensions
Who is this for?
- Those who want to develop iOS or macOS apps
- Developers planning a career in the Apple ecosystem
- Those who want to learn a modern, strongly typed language
When to use?
For iPhone, iPad, Mac, and Apple Watch apps, Swift is the only logical choice; all of Apple's new interface and system libraries are designed with Swift in mind.
If you are targeting only Android, Kotlin is more suitable; if you want to deploy to both platforms with a single codebase, Dart and Flutter are better. Although Swift is possible on the server side, the ecosystem is not as mature as Go or Node.js.
Frequently asked questions
Is a Mac required to learn Swift?
To develop a real iOS app and publish it to the store, yes: Xcode runs only on macOS and building/signing requires Apple tools. However, to learn the language itself, it is not required; the Swift compiler exists on Linux, and you can run the examples on this site directly from your browser.
What is an Optional, why do I keep seeing question marks?
An Optional makes the possibility of a variable not holding a value part of its type. When writing String?, it means "it can be text, or it can be empty". The question mark denotes this possibility, while the exclamation mark means "I am sure it has a value". Force unwrapping with an exclamation mark will crash the program if it turns out to be empty; therefore, if let or guard let is preferred.
Should I use a struct or a class?
In Swift, the default choice should be a struct: it is a value type, gets copied, and does not produce unexpected shared state. A class, on the other hand, is a reference type and is used when inheritance is required or when the same object needs to be seen as the exact same instance from multiple places.
Should I learn SwiftUI or UIKit?
For new projects, SwiftUI allows you to build interfaces with much less code, and this is Apple's direction. However, since the majority of existing apps are still written in UIKit, you will encounter both in professional life. Learning the language first, then SwiftUI, and adding UIKit as needed is a practical sequence.
Summary
Swift is the modern and strongly typed language of Apple platforms; with its optional types, it moves a common cause of crashes to compile time. In this tutorial, we progress from basic syntax to protocols and closures.