Swift from the ground up
The language of Apple, and beyond
- Beginner
- 12 chapters
- 197 min read
Learn Swift by running it: printing and top-level code, let and var with types, string interpolation, if and switch with ranges, loops, functions with labels and defaults, optionals, arrays and dictionaries with map and filter, tuples, enums with associated values, structs, protocols, closures, error handling, and a capstone report.
What You'll Learn
- Write a program that runs from the first line and prints a line of text
- Hold values in let and var, and know how whole numbers and decimals behave
- Build text with a backslash, parentheses and the everyday string tools
- Branch with if, else, switch and ranges
- Repeat work with for-in, while and repeat
- Write functions with argument labels, defaults and return values
- Handle values that may be missing with optionals, if let and guard let
- Keep lists and keyed tables, and fold them with map, filter, sort and reduce
- Group values in tuples and model choices with enums and associated values
- Model a thing with a struct, a computed property and a protocol
- Pass a closure to a function, and throw and catch an error
- Build a ranked report by filtering, sorting and summarising records
Prerequisites
- No programming experience needed — this course starts at the first line
- Any other language you know will make the early chapters quick
Vocabulary
The terms the chapters assume, in plain words.
- array
- An ordered list of values of one type, reached by position starting at zero.
- block
- A group of lines of code that run together, often inside curly brackets.
- closure
- A piece of code you can store in a value and pass to other code, remembered with the names around it.
- compiler
- The program that turns the code you write into something the machine can run, reporting mistakes before it does.
- dictionary
- A collection of key and value pairs, where each key appears once and finds its value.
- enum
- A type whose value is one of a fixed set of named cases, which may each carry extra data.
- expression
- A piece of code that produces a value, such as a sum or a call to a function.
- function
- A named piece of code that takes values and returns a result when called.
- guard
- A check that must pass before the code after it runs, leaving the scope early when it fails.
- method
- A function that belongs to a type and is called on a value of that type.
- nil
- The absence of a value, allowed only where a type says a value may be missing.
- operator
- A symbol such as plus or equals that combines values into a new one.
- optional
- A type that either holds a value or holds nothing, written with a question mark after the type.
- optional chaining
- Writing a question mark before something you reach into, so the result is nil when the value is missing.
- property
- A named value that belongs to a type or to one of its values.
- protocol
- A list of requirements that a type promises to meet, so code can work with any type that meets them.
- scope
- The region of code where a name can be used before it goes out of reach.
- string interpolation
- Putting a value inside text with a backslash and parentheses, so the value becomes part of the text.
- struct
- A type that groups related values and makes a copy whenever it is given another name or passed along.
- subscript
- A way to read or write an item in an array or dictionary by putting a key or position in square brackets.
- type
- What kind of value something is, such as a whole number or text, and what it can do.
- unwrap
- To get the value out of an optional after checking that something is there.
Course Content
- 01Your first Swift programNot started
What Swift is for, how a program is a list of steps, and how to print a line of text on the screen.
- 02Values and typesNot started
Holding numbers and text in named values, choosing between let and var, and how whole numbers and decimals behave.
- 03Text and interpolationNot started
Building text with a backslash and parentheses, joining strings, and the everyday string tools worth knowing.
- 04Decisions with if and switchNot started
Choosing a path with if and else, sorting many answers with switch, and matching ranges of numbers.
- 05Loops and rangesNot started
Repeating work with for-in, counting with ranges, and looping while a condition still holds.
- 06Functions and their labelsNot started
Naming a step, handing values in, returning a result, and the argument labels that make a call read like a sentence.
- 07OptionalsNot started
The value that may be missing, the question mark that says so, and the safe ways to open, guard and replace a missing value.
- 08Arrays and dictionariesNot started
Lists that keep order, keyed lookups, and the four tools that work on a whole collection at once.
- 09Tuples and enumsNot started
Grouping a few values without a name, and a fixed set of cases where each case can carry its own data.
- 10Structs and protocolsNot started
A struct that models a thing, properties it computes on demand, and a protocol that states what a type can do.
- 11Closures and errorsNot started
A block of code you can pass around, and how Swift reports a failure that a caller must deal with.
- 12Capstone: a records reportNot started
Modeling a record as a struct, keeping only the ones that qualify, sorting them, and printing a tidy report.
