What are primary associated types in Swift 5.7?

Published on: June 8, 2022

Protocols and associated types have always been somewhat of an interesting beast. They were hard to use sometimes, and before Swift 5.1 we would always have to resort to generics. Consider the following example: class MusicPlayer { func play(_ playlist: Collection) { /* ... */ } } This example wouldn’t compile, and it still wouldn’t today. The reason is that Collection has various associated types that must be clear if we want to use Collection. A common workaround is to use a generic: class MusicPlayer<Playlist: Collection> { func play(_ playlist: Playlist) { /* ... */ } } Instead of using...

Read more...

What’s the difference between any and some in Swift 5.7?

Published on: June 8, 2022

Protocols are an extremely important part in the Swift language, and in recent updates we've received some new capabilities around protocol and generics that allow us to be much more intentional about how we use protocols in our code. This is done through the any and some keywords. In this post, you will learn everything you need to know about the similarities and differences between these two keywords. We'll start with an introduction of each keyword, and then you'll learn a bit more about the problems each keyword solves, and how you can decide whether you should use some or...

Read more...

Presenting a partially visible bottom sheet in SwiftUI on iOS 16

Published on: June 6, 2022

This post is up to date for Xcode 14.0 Beta 1 and iOS 16 Beta 1. It supersedes a version of this post that you can find here On iOS 15, Apple granted developers the ability to present partially visible bottom sheets using a component called UISheetPresentationController. Originally, we had to resort to using a UIHostingController to bring this component to SwiftUI. With iOS 16, we don't have to do this anymore. You can make use of the presentationDetents view modifier to configure your sheets to be fully visible, approximately half visible, or some custom fraction of the screen's height....

Read more...

Formatting dates in Swift using Date.FormatStyle on iOS 15

Published on: May 27, 2022

Formatting dates in Swift using Date.FormatStyle on iOS 15 Working with dates isn’t easy. And showing them to your users in the correct locale hasn’t always been easy either. With iOS 15, Apple introduced a new way to convert Date objects from and to String. This new way comes in the form of the new Formatter api that replaces DateFormatter. As any seasoned iOS developer will tell you, DateFormatter objects are expensive to create, and therefor kind of tedious to manage correctly. With the new Formatter api, we no longer need to work with DateFormatter. Instead, we can ask a...

Read more...

Closures in Swift explained

Published on: May 23, 2022

Closures are a powerful programming concept that enable many different programming patterns. However, for lots of beginning programmers, closures can be tricky to use and understand. This is especially true when closures are used in an asynchronous context. For example, when they’re used as completion handlers or if they’re passed around in an app so they can be called later. In this post, I will explain what closures are in Swift, how they work, and most importantly I will show you various examples of closures with increasing complexity. By the end of this post you will understand everything you need...

Read more...

Debugging Network Traffic With Proxyman

Published on: May 12, 2022

Disclaimer: This post is not sponsored by Proxyman, nor am I affiliated with Proxyman in any way. I pay for my license myself, and this post is simply written as a guide to learning more about a tool that I find very important in the iOS Developer toolbox. Networking is an essential part of modern iOS applications. Most apps I’ve worked have some kind of networking component. Sometimes the networking layer involves user authentication, token refresh flows, and more. Other times, I’ll simply need to hit one or two endpoints to fetch new data or configuration files for my app....

Read more...

The difference between checked and unsafe continuations in Swift

Published on: April 24, 2022

When you’re writing a conversion layer to transform your callback based code into code that supports async/await in Swift, you’ll typically find yourself using continuations. A continuation is a closure that you can call with the result of your asynchronous work. You have the option to pass it the output of your work, an object that conforms to Error, or you can pass it a Result. In this post, I won’t go in-depth on showing you how to convert your callback based code to async/await (you can refer to this post if you’re interested in learning more). Instead, I’d like...

Read more...

Wrapping existing asynchronous code in async/await in Swift

Published on: April 24, 2022

Swift’s async/await feature is an amazing way to improve the readability of asynchronous code on iOS 13 and newer. For new projects, this means that we can write more expressive, more readable, and easier to debug asynchronous code that reads very similar to synchronous code. Unfortunately, for some of us adopting async/await means that we might need to make pretty significant changes to our codebase if it’s asynchronous API is currently based on functions with completion handlers. Luckily, we can leverage some of Swift’s built-in mechanisms to provide a lightweight wrapper around traditional asynchronous code to bring it into the...

Read more...

Comparing lifecycle management for async sequences and publishers

Published on: April 12, 2022

In my previous post you learned about some different use cases where you might have to choose between an async sequence and Combine while also clearly seeing that async sequence are almost always better looking in the examples I’ve used, it’s time to take a more realistic look at how you might be using each mechanism in your apps. The details on how the lifecycle of a Combine subscription or async for-loop should be handled will vary based on how you’re using them so I’ll be providing examples for two situations: Managing your lifecycles in SwiftUI Managing your lifecycles virtually...

Read more...

Comparing use cases for async sequences and publishers

Published on: April 12, 2022

Swift 5.5 introduces async/await and a whole new concurrency model that includes a new protocol: AsyncSequence. This protocol allows developers to asynchronously iterate over values coming from a sequence by awaiting them. This means that the sequence can generate or obtain its values asynchronously over time, and provide these values to a for-loop as they become available. If this sounds familiar, that’s because a Combine publisher does roughly the same thing. A publisher will obtain or generate its values (asynchronously) over time, and it will send these values to subscribers whenever they are available. While the basis of what we...

Read more...