Swift fundamentals

Let and var in Swift explained

Published on: July 12, 2024

Virtually every programming language will have some means to define properties; Swift does too. We have two approaches to defining a property in Swift. We can use a var or a let. The code below shows how we can define a var or a let as a member of a class: class Member { let id: UUID var name: String init(name: String) { self.id = UUID() self.name = name } } This class has two properties. One is a let, the other is a var. If you're coming from a Javascript background you might expect that there's a third option...

Read more...

How to decide between a Set and Array in Swift?

Published on: May 15, 2024

Collections are a key component in any programming language. We often refer to collections as Array or Set but there are several other kinds of collections in programming like String (often a collection of type Character) and ArraySlice (referring to a part of an array). In this post, I’d like to explore two of the most common collection types; Set and Array. We’ll take a look at the key characteristics for each and we’ll explore use cases where we can use each. We’ll cover the following topics: Understanding Array’s key characteristics Understanding Set’s key characteristics Exploring performance considerations Use cases...

Read more...

Swift’s “if” and “switch” expressions explained

Published on: May 14, 2024

In Swift, we sometimes want to assign a property based on whether a certain condition is true or false, or maybe based on the value of an enum. To do this, we can either make a variable with a default value that we change after checking our condition or we define a let without a value so we can assign a value based on our conditions. Alternatively, you might have used a ternary expression for simple assignments based on a conditional check. Here’s what a ternary looks like: let displayName = object.isManaged ? object.managedName : object.name This code isn’t easy...

Read more...

What are enums in Swift?

Published on: May 8, 2024

Swift comes with types of objects that we can use to write type declarations. They all have their own distinct features, upsides, and downsides. In this post I’d like to zoom in on the enum type so you can get a sense of what enums are, and when they can be useful. In this post we’ll cover the following topics: Understanding the basics of enums Knowing when an enum should be used Avoiding enum overuse Let's jump right in! Understanding the basics of enums In Swift, enums are values types that are declared using the enum keyword. Every possible value...

Read more...

What is defer in Swift?

Published on: April 29, 2024

Sometimes, we write code that needs set some state or perform some work at the start of a function and at the end of that same function we might have to reset that state, or perform some cleanup regardless of why we’re exiting that function. For example, you might have a function that creates a new Core Data object and depending on whether you’re able to enrich the object with data from the network you want to exit the function early. Regardless of how and why you exit the function, you want to save your newly created object. Writing our...

Read more...

Deciding between a computed property and a function in Swift

Published on: April 26, 2024

In Swift, we can use computed properties to derive a value from other values defined on the same object. Being able to do this is super convenient because it means that we don’t have to manually make sure that we update derived properties every time one of the “source” values changed. We’ll just recompute the property every time it’s accessed! If you prefer to learn from video, here's the companion video for this blog post: This is very similar to having a function that takes no arguments and returns a value: struct User { let givenName: String let familyName: String...

Read more...

if case let in Swift explained

Published on: April 26, 2024

In Swift, we can use the case keyword in multiple places. Most commonly, a case is used in switched but since you’re here, you might have seen a case in combination with an if statement. In this post, we’ll explore different places where we can use the case keyword to perform something called pattern matching in Swift. Pattern matching is a powerful feature of Swift that allows us to perform highly elegant checks to see if a given type matches a certain value. In this post, we’ll explore a specific kind of pattern matching in Swift; the if case let...

Read more...

What are lazy vars in Swift?

Published on: April 23, 2024

Sometimes when you’re programming you have some properties that are pretty expensive to compute so you want to make sure that you don’t perform any work that you don’t absolutely must perform. For example, you might have the following two criteria for your property: The property should be computed once The property should be computed only when I need it If these two criteria sound like what you’re looking for, then lazy vars are for you. A lazy variable is defined as follows: class ExamResultsAnalyser { let allResults: [ExamResult] lazy var averageGrade: Float = { return allResults.reduce(0.0, { total, result...

Read more...

Deciding between a for loop or forEach in swift

Published on: April 23, 2024

Swift offers multiple ways to iterate over a collection of items. In this post we’ll compare a normal for loop to calling forEach on a collection. Both for x in collection and collection.forEach { x in } allow you to iterate over elements in a collection called collection. But what are their differences? Does one outperform the other? Is one better than the other? We’ll find out in this post. Using a regular for loop I’ve written about for loops in Swift before so if you want an in-depth look, take a look at this post. A regular for loop...

Read more...

An extensive guide to sorting Arrays in Swift

Published on: April 7, 2021

When you're working with Arrays in Swift, it's likely that you'll want to sort them at some point. In Swift, there are two ways to sort an Array: Through the Comparable implementation for each element in your array By providing a closure to perform a manual/specialized comparison between elements If you have a homogenous array of elements, for example [String], you can rely on String's implementation of Comparable to sort an array of String in some sensible manner. There are two ways to sort an array of Comparable elements: var strings = ["Oh", "Hello", "World", "This", "Is", "An", "Unsorted", "Array"]...

Read more...