Swift fundamentals

Effectively using static and class methods and properties

Published on: December 7, 2019

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

Published on: November 6, 2019

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

When to use map, flatMap and compactMap in Swift

Published on: October 23, 2019

Any time you deal with a list of data, and you want to transform the elements in this list to a different type of element, there are several ways for you to achieve your goal. In this week’s Quick Tip, I will show you three popular transformation methods with similar names but vastly different applications and results. 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...

Read more...

An in-depth look at for loops in Swift

Published on: October 16, 2019

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 week's Quick Tip, 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. Understanding for loops Commonly when you write...

Read more...

Finding the difference between two Arrays

Published on: October 6, 2019

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

Published on: July 13, 2015

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

Published on: June 30, 2015

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