Swift Tutorial
Apple's language for iOS, macOS and beyond.
Getting started3 lessons
Basics26 lessons
- 4Swift Variables and Constantslet creates a constant and var a variable.
- 5Swift Data TypesCommon types: Int, Double, Float, Bool, String and Character.
- 6Swift StringsStrings use \(value) interpolation.
- 7Swift OptionalsAn optional may hold a value or nil, written with ?.
- 8Swift Type ConversionConvert with initialisers: Int("42") returns an optional, String(42) always works.
- 9Swift TuplesA tuple groups several values, possibly of different types, and can name its parts.
- 10Swift BooleansBool is true or false.
- 11Type Safety and AliasesSwift is type safe: a variable keeps its type.
- 12Swift CommentsUse // for one line and /* */ for several lines.
- 13Swift Numbers and MathInt and Double support the usual math.
- 14let and varlet makes a constant and var a variable.
- 15Basic TypesInt, Double, Float, Bool, String and Character.
- 16OptionalsAn optional holds a value or nil.
- 17String Interpolation\(expression) inserts values.
- 18TuplesA tuple groups values.
- 19OperatorsArithmetic, comparison, logical, range and nil coalescing operators.
- 20Type Castingas? safely casts, as! forces and is tests a type.
- 21Math and RandomFoundation has sqrt, pow, abs, floor.
- 22Reading InputreadLine() returns an optional String from standard input.
- 23Type Aliases and Constantstypealias gives a type a new name.
- 24print Optionsprint takes separator and terminator.
- 25Comments// is a line comment and /* */ a block that can nest.
- 26Number Literals and OverflowUse underscores, binary, hex and octal literals.
- 27Identity and Equality== compares values and === compares class instance identity.
- 28Optional Binding PatternsCombine if let, guard let and multiple bindings.
- 29assert and preconditionassert checks during development only.
Operators2 lessons
Control flow11 lessons
- 32Swift If and ElseConditions need no parentheses but braces are required.
- 33Swift Switchswitch must cover every case and there is no implicit fallthrough.
- 34Swift Loopsfor in loops over ranges and collections.
- 35Swift Guardguard checks a condition and exits early if it fails, keeping the main path flat.
- 36if and elseConditions need no parentheses and braces are required.
- 37switchswitch must be exhaustive and does not fall through.
- 38for in Loopsfor in walks ranges, arrays, dictionaries and strings.
- 39while and repeatwhile checks first and repeat while runs at least once.
- 40guardguard exits early when a condition fails.
- 41break, continue and Labelsbreak leaves a loop, continue skips a round and labels target outer loops.
- 42Rangesa...b is closed and a..<b is half open.
Collections14 lessons
- 43Swift ArraysAn array is an ordered list of one type.
- 44Swift DictionariesA dictionary maps unique keys to values.
- 45map, filter and reduceCollections support functional operations: map transforms, filter keeps matches, reduce co.
- 46Swift SetsA Set stores unique, unordered values with fast lookup.
- 47Sorting and Searchingsorted returns a new array and sort() sorts in place.
- 48ArraysArrays are ordered and typed.
- 49Array Operationsmap, filter, reduce, sorted, contains, firstIndex and joined are common.
- 50SetsSet stores unique values with fast membership.
- 51DictionariesA dictionary maps keys to values.
- 52Slices and RangesA slice is a view of part of an array.
- 53Grouping and ZippingDictionary(grouping:by:) buckets items.
- 54compactMap and flatMapcompactMap drops nils and flatMap flattens nested arrays.
- 55StringsStrings are Unicode aware.
- 56String IndexingUse String.Index with startIndex, index(_:offsetBy:) or convert to an Array.
Functions10 lessons
- 57Swift FunctionsDeclare with func.
- 58Swift ClosuresA closure is a function without a name in braces: { params in body }.
- 59Variadic and inout ParametersA variadic parameter accepts many values.
- 60Functionsfunc declares a function with typed parameters and a return type.
- 61Default and Variadic ParametersGive defaults and use .
- 62inout Parametersinout lets a function change the caller variable.
- 63ClosuresA closure is { params in body }.
- 64Functions as ValuesFunctions are values.
- 65RecursionA function may call itself with a base case.
- 66Nested Functions and ScopeFunctions can live inside functions and capture local variables.
Object oriented Swift20 lessons
- 67Swift StructsA struct is a value type with properties and methods and a free memberwise initialiser.
- 68Swift ClassesA class is a reference type that supports inheritance.
- 69Swift PropertiesStored properties hold values, computed properties calculate them with get and set, and pr.
- 70Swift InheritanceA class can inherit from one superclass.
- 71Swift ProtocolsA protocol lists requirements a type must meet, like an interface.
- 72Swift ExtensionsAn extension adds methods, computed properties and protocol conformance to an existing typ.
- 73Swift EnumsAn enum defines a group of related values.
- 74Access ControlKeywords private, fileprivate, internal (default), public and open control who can use a m.
- 75StructsStructs are value types.
- 76ClassesClasses are reference types with inheritance.
- 77PropertiesStored properties hold values.
- 78Methods and mutatingMethods on structs that change the instance must be marked mutating.
- 79InheritanceA class inherits with a colon.
- 80ProtocolsA protocol lists requirements.
- 81ExtensionsExtensions add methods and computed properties to existing types.
- 82EnumsEnums define a set of cases that can carry values and methods.
- 83GenericsGenerics write code for any type with constraints.
- 84Optional Chaininga?.b?.c stops at the first nil and returns nil.
- 85Access Controlopen, public, internal, fileprivate and private control visibility.
- 86InitialisersDesignated initialisers set every property.
Errors and memory13 lessons
- 87Swift Error HandlingErrors are types that conform to Error.
- 88Memory and ARCSwift manages class memory with automatic reference counting.
- 89Errors with throw and do catchFunctions that throw are marked throws and called with try.
- 90try? and try!try? turns errors into nil and try! crashes on error.
- 91deferdefer runs code when the scope ends, great for cleanup.
- 92Memory and ARCSwift counts references to class instances.
- 93Result TypeResult<Success, Failure> carries a value or an error.
- 94Codable and JSONCodable converts types to and from JSON with JSONEncoder and JSONDecoder.
- 95Dates and FilesDate, DateFormatter and FileManager handle dates and files.
- 96async and awaitasync functions suspend and await waits for them.
- 97Practice: Word FrequencySplit, group and sort.
- 98Practice: FizzBuzzCombine a range with switch on a tuple.
- 99Practice: Bank AccountCombine struct, mutating and errors.
Advanced SwiftPremium5 lessons
- 100Swift GenericsGenerics let functions and types work with any type while staying safe.
- 101Swift Concurrencyasync functions can suspend and await waits for results without blocking threads.
- 102Property Wrappers and Result BuildersA property wrapper adds reusable behaviour to properties, such as @State in SwiftUI.
- 103Opaque Types and somesome Protocol returns a specific type without naming it.
- 104Codable and JSONConform to Codable to convert types to and from JSON with JSONEncoder and JSONDecoder.
Swift in practicePremium9 lessons
- 105SwiftUI BasicsSwiftUI builds interfaces with simple declarative code.
- 106SwiftUI State and Binding@State stores local view data and changes redraw the view.
- 107SwiftUI Lists and NavigationList shows rows, ForEach builds repeated views and NavigationStack moves between screens w.
- 108Networking with URLSessionURLSession downloads data.
- 109Saving DataUserDefaults stores small settings, FileManager works with files, and SwiftData or Core Da.
- 110Testing SwiftXCTest and Swift Testing check your code.
- 111Swift Package ManagerSwiftPM manages dependencies and builds.
- 112Publishing to the App StoreYou need an Apple Developer account, a signed build, app icons, screenshots and a privacy .
- 113Swift Best PracticesPrefer let and structs, use optionals instead of sentinel values, name functions by what t.