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

How to use print statements in SwiftUI

Debugging SwiftUI views can feel a little challenging due to its declarative nature. In this post, we’ll explore how to add print statements into your SwiftUI code to observe state changes, view updates, and data flow. Why use print in SwiftUI? SwiftUI’s declarative paradigm means you describe what your UI should look like, and the framework handles when to update it. This can make it easier to reason about but harder to debug lifecycle events and UI-related state changes. print statements give you: ...

How to build URLs in Swift

Building URLs safely and expressively is a common requirement in Swift applications—especially when dealing with REST APIs. In this post, we’ll explore four elegant approaches to construct URLs in Swift: manual string interpolation, Foundation’s URLComponents, a custom URL‐builder DSL, and an enum‐driven router. By the end, you’ll have a clear sense of trade‐offs and patterns you can adopt in your own codebase. Manual String Interpolation The simplest approach is to build the URL by concatenating strings: ...

Unraveling Swift Result Builders

In Swift 5.4, a powerful new feature was introduced that revolutionised how we can work with composable pieces of code: Result Builders. If you’ve written any SwiftUI code, you’ve probably already encountered this feature without realizing it. Result Builders underpin much of the magic that makes SwiftUI’s declarative syntax possible. However, their use isn’t limited to SwiftUI. In this blog post, we’re going to delve into Result Builders, what they are, and how you can use them to build more expressive and powerful APIs. ...

June 11, 2023 · Mike Gopsill ·  Swift

Examples of Swift Property Wrappers

Swift’s property wrappers are a powerful tool that allows developers to change how properties are stored and manipulated while keeping their interfaces clean and consistent. This post will discuss some practical use cases for property wrappers. Let’s get started! 1. UserDefault Wrapper UserDefaults is a straightforward mechanism to store small amounts of data persistently. We can simplify UserDefaults interactions with a UserDefault property wrapper: @propertyWrapper struct UserDefault<T> { let key: String let defaultValue: T init(_ key: String, defaultValue: T) { self.key = key self.defaultValue = defaultValue } var wrappedValue: T { get { return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue } set { UserDefaults.standard.set(newValue, forKey: key) } } } You can now store and retrieve UserDefaults values effortlessly: ...

June 2, 2023 · Mike Gopsill ·  Swift

Swift Property Wrappers

Property Wrappers were introduced to Swift in 5.1. Initially, they can seem a bit mystifying. However, they’re a powerful tool, helping to streamline your code and make it more expressive. Today, we’ll demystify Property Wrappers and learn how to use them in Swift. What Exactly is a Property Wrapper? Think of a Property Wrapper as a special kind of structure, class, or enumeration that “wraps” around a property in your code. This wrapper can add extra behaviour to the property, making it easier to manage and modify. ...

June 1, 2023 · Mike Gopsill ·  Swift

Understanding Modularisation and Dependencies in iOS and Swift

As software projects grow, so does their complexity. Managing this complexity is one of the most critical aspects of software development. One way of handling this complexity is through the principle of modularisation. In this post, we’ll delve deeper into the concept of modularisation and its counterpart, dependencies, in the context of iOS development with Swift. Modularisation in iOS In its essence, modularisation is the process of dividing a software system into separate, independent modules. Each module encapsulates a specific part of the system’s functionality and exposes a well-defined interface that other modules can use. ...

The Power of UIAppearance - Styling iOS Apps

UIAppearance is an underappreciated gem in UIKit’s toolbox. It provides an elegant way to style UI elements globally across your app, thereby keeping your codebase DRY (Don’t Repeat Yourself) and maintainable. In this post, we’ll dive into UIAppearance and learn how to harness its power to style our iOS apps effectively. What is UIAppearance? UIAppearance is a protocol in UIKit that allows you to customise the appearance of all instances of a class. You can set global styles for user interface elements, which will be applied to all instances of that class. ...

May 28, 2023 · Mike Gopsill ·  UIKit

Understanding Task and Child Task in Swift's Structured Concurrency

In the realm of programming, the ability to handle concurrent tasks is vital to creating applications that are both performant and user-friendly. The introduction of Swift’s Structured Concurrency has revolutionised the way developers handle async operations. This post will delve into the intricacies of tasks and child tasks in Swift’s Structured Concurrency and provide a comprehensive understanding of their uses. The Power of Tasks In Swift’s Structured Concurrency model, tasks are the building blocks of asynchronous operations. Marked by the async keyword, these tasks denote computations that are performed asynchronously. ...