Quick Tip

What does “atomic” mean in programming?

Published on: January 6, 2021

When you're learning about databases or multithreaded programming, it's likely that you'll come across the term "atomic" at some point. Usually you'll hear the term in the context of an operation. For example, an atomic read / write operation. Or atomic access to a property. But what does this mean? Generally, you can summarize atomic as "one at a time". For example, when accessing or mutating a property is atomic, it means that only one read or write operation can be performed at a time. If you have a program that reads a property atomically, this means that the property...

Read more...

Formatting dates in the user’s locale using DateFormatter in Swift

Published on: October 15, 2020

Working with dates is hard, there is no doubt about that. And formatting dates properly for every user of your app is no easier (if you want to do everything manually). Luckily, the system can help us. For example, in the US one would write "October 15" while in The Netherlands we write 15 oktober. Note that the order of the date and the month is different, the spelling of the month is different and the capitalization is different too. The DateFormatter in iOS will handle a lot of this for you. For example, if you'd use the following code...

Read more...

Using multi-colored icons in iOS 14 with SF Symbols 2

Published on: June 28, 2020

Apple introduced SF Symbols in iOS 13. SF Symbols allow developers to easily integrate icons in their apps. The SF Symbols icons integrate really well with the default system font, and provide a consistent look throughout the system. In iOS 14, Apple added over 750 new icons to the SF Symbols library for developers to use in their apps. Additionally, Apple has expanded SF Symbols to include multi-colored icons. For a full overview of the available SF Symbols that are available, including the newly added and multicolor symbols, download the SF Symbols 2 app from Apple's SF Symbols page. To...

Read more...

What’s the difference between Float and Double in Swift

Published on: June 10, 2020

A Double and Float are both used to represent decimal numbers, but they do so in slightly different ways. If you initialize a decimal number in Swift using as shown below, the Swift compiler will assume that you meant to create a Double: let val = 3.123 // val is inferred to be Double The reason for this is that Double is the more precise type when comparing it to Float. A Float holds a total of 8 positions, or 32 bits. Since Double is more precise, it can hold more positions. It uses 64 bits to do this. In...

Read more...

What’s the difference between catch and replaceError in Combine?

Published on: May 29, 2020

There are several ways to handle errors in Combine. Most commonly you will either use catch or replaceError if you want to implement a mechanism that allows you to recover from an error. For example, catch is useful if you want to retry a network operation with a delay. The catch and replaceError operators look very similar at first glance. They are both executed when an error occurs in your pipeline, and they allow you to recover from an error. However, their purposes are very different. When to use catch The catch operator is used if you want to inspect...

Read more...

Reclaim disk space by deleting old iOS simulators and Device Support files

Published on: May 24, 2020

After using a MacBook that runs Xcode for a few years it's likely that your disk space is starting to fill up good. A large part of this disk space can be occupied by Device Support files that are used by Xcode for older iOS versions, or by iOS simulators that are no longer available on your machine. To clean these files up you can do the following: Go to your Terminal and type open ~/Library/Developer/Xcode/iOS\ DeviceSupport Delete folders for iOS versions that you no longer need to support. Do the same with open ~/Library/Developer/Xcode/watchOS\ DeviceSupport Clean up unavailable simulators...

Read more...

Throttle network speeds for a specific host in Charles

Published on: May 21, 2020

Sometimes you'll want to test whether your app works properly under poor networking conditions. One way to test this is Apple's Network Link Conditioner. Unfortunately, this will slow internet speeds for your entire machine to a crawl which can be counterproductive. Especially if you want to throttle your app for a longer period of time. If you have Charles installed to debug your app's network traffic, you can use it to throttle network speeds for the entire system, or for a selection of hosts which is exactly what we're looking for. To enable throttling in Charles you can either go...

Read more...

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

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

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