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. ...

Swift Control Flow

Today we’ll look at control flow statements like if, for, and switch. These statements are the decision makers of Swift, allowing you to create dynamic software. Understanding control flow means you can write code that can branch, loop, and make decisions based on the data it encounters. What is Control Flow? Control flow determines the order in which your code executes. Instead of just running line by line from top to bottom, control flow lets you skip sections, repeat operations, or choose between different paths based on conditions. It’s what transforms a simple list of instructions into intelligent software. ...

Swift Operators

If you’ve been working with Swift for a while, you’ve probably been using operators without giving them much thought. They’re the symbols that let you perform calculations, compare values, and combine logic. Some examples include +, -, ==, and &&. Understanding operators properly will help you write cleaner, more expressive Swift code. What Are Operators? An operator is a symbol that tells Swift to perform a specific operation on one, two, or three values. They are like the verbs of programming, that perform action on data types ...

Swift Basic Data Types

If you’re just getting started with Swift, you’ll quickly realise that everything revolves around data—numbers, text, true/false values, lists of things. Understanding Swift’s basic data types is like learning the alphabet before you write sentences. This post covers the essential building blocks you’ll use in every Swift project. What are Data Types? A data type defines the kind of data you’re working with and what actions you can perform on it. Are you handling a number or a name? A true/false flag or a list of groceries? Swift is a strongly typed language, which means you must be clear about what types you’re working with. Swifts compiler will catch errors early if you make mistakes. ...

Swift Variables & Constants

Understanding variables and constants is one of the first steps in learning Swift. These building blocks will enable you to store, update, and manage data in your programs. In this post, we’ll cover what variables and constants are, how to use them, and why you should prefer one over the other depending on your use-case. What Are Variables? A variable is a named space in memory where you can store information that might change over time. ...

Searchable Folders in SwiftUI

If you haven’t met OutlineGroup in SwiftUI yet, picture Finder’s sidebar rendered in SwiftUI: click a chevron, folder children appear. I recently tried to get this working with search. I wanted folders to open and close depending on whether the contained valid results… After much wrestling and many coffees I gave up on OutlineGroup in favour of a tiny Node model, a ExplorerStore viewmodel, and some recursion. The result? A searchable file‑browser that behaves at least as my mental model thinks it should. ...

Stopping SetNSColor(CGContext*, void const*) Crashes in UIKit

TL;DR I recently encountered a crash on SetNSColor(CGContext*, void const*) with [__SwiftValue CGColor]: unrecognized selector sent to instance. It took a while to find the root cause as the stack trace lacked a lot of detail. Ultimately it was a SwiftUI Color in a UIKit API that expects a UIColor (or raw CGColor). In this case I passed a SwiftUI Color via the .foregroundColor attribute of an NSAttributedString that UIKit then rendered. ...

Using Context Menu with Previews in SwiftUI

SwiftUI’s contextMenu modifier is a simple API that does a lot for us. I wrote about building context menus in both UIKit and SwiftUI recently. I did not go into detail about support for the custom previews that landed in iOS 16, iPadOS 16 and macOS 13. Let us fill that gap. Recap: Context menus in SwiftUI A context menu is equivalent of a right-click on touch devices. Using long‑press (or Control‑click on the Mac) on a view reveals a menu of options exactly where the user needs it. In pure SwiftUI we use .contextMenu to create these menus: ...

How to use Context Menus in UIKit

In this post we’re going to add context menus to a UIKit UITableView. The end goal is to have something that looks like this: Why Use Context Menus? Context menus are great for keeping things tidy while still offering useful features. They: Keep your UI minimal Only show options when needed Feel natural with the iOS long-press gesture Save you adding extra buttons all over the UI – the menu keeps them tucked out of sight until needed. What We’re Making We’ll show a short list of fruit. When we long-press any item we will show a context menu with three options: ...

How to use Context Menus in SwiftUI

The aim for this post to build a very simple List view. When you long-press a row you will see a classic iOS context menu. It will look a little like this: Why Context Menus? Context menus give users an easy way to discover secondary actions right where they’re looking. They: Keep the primary UI clean. Surface actions only when they’re relevant. Feel familiar thanks to the long-press gesture we already use across iOS. What We’ll Build We’ll create a simple list of fruit. A long-press on any row reveals three actions: ...