Swift Classes & Inheritance

Classes are a fundamental concept in Object-Oriented Programming (OOP) and a cornerstone of Swift. In OOP, we model complex systems by creating “objects”—self-contained units that bundle data (properties) and behavior (methods). A class is the blueprint for creating these objects. Unlike structures, classes have two key characteristics: Inheritance: A class can inherit functionality from a parent class, allowing you to build hierarchies of related types. Reference Semantics: Classes are reference types. When you pass a class instance around, you’re passing a reference to the same underlying object in memory. Let’s look at a basic class: ...

Swift Structures

Let’s talk about structures, or structs as they’re known in code. Structures are a fundamental building block in Swift. They are versatile and widely used, from simple data containers to more complex types with their own behavior. What are Structures? A structure is a way to group related values together into a named type. Think of it like a blueprint for creating structures that hold specific kinds of data. For example, you could define a struct to represent a 2D point with x and y coordinates, or a struct to hold information about a book, like its title, author, and pageCount. ...

Swift Enumerations

Enums provide a way to define a common type for a group of related values. Enums create distinct cases for these values. Then you can work with, switch over and iterate through these distinct cases, making your code much more clear. What are Enumerations? Think of enums as a way to create your own custom set of options. For example, the days of the week, the suits in a deck of cards, or the different states an application can be in. ...

Swift Optionals

When you start learning Swift, one of the concepts you’ll encounter early on is optionals. Optionals are a powerful feature of Swift that help you write safer and more robust code by explicitly handling the possibility that a value might be missing. This post will explain what optionals are, why they are important, and how to work with them. What is an Optional? In Swift, an optional is a type that can hold either a value or no value at all. When an optional doesn’t have a value, it’s said to be nil. Think of it like a box: the box might contain an item, or it might be empty. ...

Swift Closures

Closures are self-contained pieces of code that you can store and pass around your codebase. Think of them as unnamed functions that can capture and store references to any constants and variables from the context in which they’re defined. If you’ve used functions in Swift, then you’ll soon grasp closures as they are actually a special case of closures. Closures are used very often in iOS development. You’ll see them in: completion handlers, animations, collection methods like map and filter, and SwiftUI view builders. Let’s look at them in more detail. ...

Swift Functions

Functions are reusable blocks of code that perform specific tasks. They’re fundamental to organising your code, avoiding repetition, and making your programs easier to understand and maintain. Think of a function as a mini-program that takes some input, does something useful with it, and optionally gives you back a result. In Swift, functions are first-class citizens—you can pass them around, store them in variables, and use them just like any other data type. ...