Swift fundamentals

Reversing an Array in Swift

Updated on: April 23, 2024

You can reverse an Array, and any other Collection in Swift using the reverse method. For example var input = [1, 2, 3] print(input) // [1, 2, 3] input.reverse() print(input) // [3, 2, 1] The code above takes an array (input) and reverses it in-place using the reverse() method. This only works if your array is mutable. If you want to reverse an immutable array that's defined as let, or if you don't want to alter the original input you can use reversed() instead of reverse(): var input = [1, 2, 3] print(input) // [1, 2, 3] var reversed =...

Read more...

Effectively using static and class methods and properties

Updated on: November 4, 2024

Swift allows us to use a static prefix on methods and properties to associate them with the type that they’re declared on rather than the instance. We can also use static properties to create singletons of our objects which, as you have probably heard before is a huge anti-pattern. So when should we use properties or methods that are defined on a type rather than an instance? In this blog post, I’m going to go over several use cases of static properties and methods. Once we’ve covered the hardly controversial topics, I’m going to make a case for shared instances...

Read more...

When to use weak self and why

Updated on: April 26, 2024

We all want to write good, beautiful and stable code. This includes preventing memory leaks, which we can, using [weak self] when writing a closure that needs access to self. But what's the real reason for needing this weak capture? And do we need it all the time? In this week's Quick Tip, I want to help you find an answer to this question so you can make more informed decisions about your capture lists in the future. This post contains the following topics: Understanding what a capture list is Understanding different kinds of captures Knowing when to rely on...

Read more...

Map, flatMap and compactMap in Swift explained with examples

Updated on: July 7, 2025

Transforming a set of data in Swift can be done using several different mechanisms. In this post, we're going to take a look at three functions that are defined on Sequence with very similar names. By the end of this post, you will understand exactly what map, flatMap and compactMap are and what they do. You will also be able to decide which flavor of map to use depending on your goals. Let’s dive right in by exploring the most straightforward of the three; map. How and when to use map? Any time you have a set of data where...

Read more...

For loops in Swift explained with examples

Updated on: July 7, 2025

In Swift, there is more than one way to loop over a list of elements. If we only consider the loops that aren't intended to create new lists or have side effects, you end up with two types of loops that are very similar; forEach and for item in list, or the for loop. In this post, I will explain the difference between for and forEach loops, and you I will show some examples that should help you make a decision picking one of either loop methods, depending on your needs. For loop basics explained Commonly when you write code...

Read more...

Finding the difference between two Arrays

Updated on: April 23, 2024

Many applications work with data, often they are built to retrieve data and display this data to the user in a table view, collection view, list (if you're using SwiftUI) or a different kind of component. It's not uncommon for this data to change and when it does you might be interested in figuring out what elements were added to the data and which items were removed. This isn't always straightforward so up until now you might have written code something like the following: func didFetchNewRecipes(_ newRecipes: [Recipe]) { recipes = newRecipes tableView.reloadData() } Simple and effective, much easier than...

Read more...

Why you should avoid force unwrapping in Swift

Updated on: April 26, 2024

Whenever I'm programming, I have a goal in mind, generally a problem to solve. I want my solutions to be simple, yet elegant and reliable. Thankfully, Swift is a great language for this. The language is safe, its syntax is beautiful with great readability. The way Swift handles nullability with Optional contributes greatly to its safety. Can you imagine having a language where you don't know whether something could be nil? Well... languages like Objective-C and Java required developers to constantly check for null or nil values to prevent crashes. I'm sure you can imagine that this would go wrong...

Read more...

Find every other element in an array with Swift

Updated on: April 23, 2024

There are times when you need to extract a subset of an array. For example, you might need to find all elements in an array at an odd index. Or maybe you need all items at an even index. In other words, you're looking for every "other" element in an array. This might seem like a non-trivial task and you may have tried this before using a for loop as follows: var itemsAtEvenIndices = [Int]() let allItems = [1, 2, 3, 4, 5, 6, 7] var index = 0 for item in allItems { if index % 2 == 0...

Read more...