Recent articles

Jump to a random post

How to have more than one type of cell in a Collection View

Published on: May 19, 2020

Collection views in iOS are awesome. You can use them to build complex custom layouts and since iOS 13 you can use Compositional Layouts to quickly build good looking layouts that would take forever to accomplish on iOS 12 and below. But what if you want to use more than one type of cell in your layout? If you're building your app without storyboard you register collection view cells using the register method on UICollectionView. If you want to use more than one cell type, all you need to do is call register multiple times: collectionView.register(CellTypeOne.self, forCellWithReuseIdentifier: "CellTypeOne") collectionView.register(CellTypeTwo.self, forCellWithReuseIdentifier:...

Read more...

What is type erasure in Swift? An explanation with code samples

Published on: May 18, 2020

Swift's type system is (mostly) fantastic. Its tight constraints and flexible generics allow developers to express complicated concepts in an extremely safe manner because the Swift compiler will detect and flag any inconsistencies within the types in your program. While this is great most of the time, there are times where Swift's strict typing gets in the way of what we're trying to build. This is especially true if you're working on code that involves protocols and generics. With protocols and generics, you can express ideas that are insanely complex and flexible. But sometimes you're coding along happily and the...

Read more...

Getting started with testing your Combine code

Published on: May 11, 2020

A question that often comes up when folks get into learning Combine is "how do I test code that uses Combine?". In this week's post, I will briefly explain the basics of testing Combine code. I will assume that you already know the basics of testing and Combine. If you're just getting started with both topics or would like a refresher I can recommend that you take a look at the following resources: My series of posts on testing My series of posts on Combine My Practical Combine book if you want to learn a lot more about Combine, and...

Read more...

Creating type-safe identifiers for your Codable models

Published on: May 4, 2020

Note: After publishing this article, it has been brought to my attention that the folks from @pointfreeco have a very similar solution for the problems I outline in this post. It's called tagged and implements the same features I cover in this post with several useful extensions. If you like this post and plan to use the concepts I describe, you should take a look at tagged. It seems that on the Swift forums, there are a couple of topics that come up regularly. One of these topics is the newtype for Swift discussion. Last week, I saw a new...

Read more...

Why your @Atomic property wrapper doesn’t work for collection types

Published on: April 20, 2020

A while ago I implemented my first property wrapper in a code base I work on. I implemented an @Atomic property wrapper to make access to certain properties thread-safe by synchronizing read and write access to these properties using a dispatch queue. There are a ton of examples on the web that explain these property wrappers, how they can be used and why it's awesome. To my surprise, I found out that most, if not all of these property wrappers don't actually work for types where it matters most; collection types. Let's look at an example that I tweeted about...

Read more...

Changing a publisher’s Failure type in Combine

Published on: April 15, 2020

One of Combine's somewhat painful to work with features is its error mechanism. In Combine, publishers have an Output type and a Failure type. The Output represents the values that a publisher can emit, the Failure represents the errors that a publisher can emit. This is really convenient because you know exactly what to expect from a publisher you subscribe to. But what happens when you have a slightly more complicated setup? What happens if you want to transform a publisher's output into a new publisher but the errors of the old and new publishers don't line up? The other...

Read more...

An introduction to Big O in Swift

Published on: April 13, 2020

Big O notation. It's a topic that a lot of us have heard about, but most of us don't intuitively know or understand what it is. If you're reading this, you're probably a Swift developer. You might even be a pretty good developer already, or maybe you're just starting out and Big O was one of the first things you encountered while studying Swift. Regardless of your current skill level, by the end of this post, you should be able to reason about algorithms using Big O notation. Or at least I want you to understand what Big O is,...

Read more...

Using Closures to initialize properties in Swift

Published on: April 8, 2020

There are several ways to initialize and configure properties in Swift. In this week's Quick Tip, I would like to briefly highlight the possibility of using closures to initialize complex properties in your structs and classes. You will learn how you can use this approach of initializing properties, and when it's useful. Let's dive in with an example right away: struct PicturesApi { private let dataPublisher: URLSession.DataTaskPublisher = { let url = URL(string: "https://mywebsite.com/pictures")! return URLSession.shared.dataTaskPublisher(for: url) }() } In this example, I create a URLSession.DataTaskPublisher object using a closure that is executed immediately when PicturesApi is instantiated. Even though...

Read more...

How to use SF Symbols in your apps

Published on: April 6, 2020

It’s been a while since Apple announced SF Symbols at WWDC 2019 and I remember how excited everybody was about them. The prospect of having an easy to integrate set of over 1,500 icons that you can display in nine weights sounds very appealing and has helped me prototype my ideas much quicker with good looking icons than ever before. I haven’t heard or seen much content related to SF Symbols since they came out and I realized I hadn’t written about them at all so I figured that I’d give you some insight into SF Symbols and how you...

Read more...

Find and copy Xcode device support files

Published on: April 1, 2020

Every once in a while I run into a situation where I update my iPhone to the latest iOS before I realize I'm still using an older version of Xcode for some projects. I usually realize this when Xcode tells me that it "Could not locate device support files". I'm sure many folks run into this problem. Luckily, we can fix this by copying the device support files from the new Xcode over to the old Xcode, or by grabbing the device support files from an external source. Copying device support files if you already have the latest Xcode installed...

Read more...