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

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

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