The Blog

How and when to use callAsFunction in Swift 5.2

A new Swift 5.2 feature is the ability to call instances of types as functions. Or, as the Swift Evolution proposal calls it “Callable values of user-defined nominal types”. The very short description of this feature is that it allows you to call instances of any type that has a callAsFunction method implemented as if it’s a function: struct InvestmentsCalculator { let input: Double let averageGrowthPerYear = 0.07 func callAsFunction(years: Int) -> Double { return (0..<years).reduce(input, { value, _ in Read more…

Testing push notifications in the Simulator with Xcode 11.4

Xcode 11.4 is currently still in beta, keep in mind that you can’t use this version of Xcode to ship apps to the store. Read my guide on installing multiple Xcode versions alongside each other if you want to play with the Xcode 11.4 beta while keeping your copy of 11.3.1 around as well. For years we’ve had to resort to using physical devices when testing push notifications. With Xcode 11.4, Apple finally gives developers the tools needed to test Read more…

Using Promises and Futures in Combine

So far in my Combine series I have mostly focussed on showing you how to use Combine using its built-in mechanisms. I’ve shown you how Combine’s publishers and subscribers work, how you can use Combine for networking, to drive UI updates and how you can transform a Combine publisher’s output. Knowing how to do all this with Combine is fantastic, but your knowledge is also still somewhat limited. For example, I haven’t shown you at all how you can take Read more…

Five ways to get better at receiving feedback

When I just started my career as a developer there was a lot I didn’t know yet. There also were a lot of things I didn’t understand or had never done before. I have always been lucky enough to work in places where I was able to learn and grow as needed, and most importantly, I was allowed to make mistakes. One of the hardest things wasn’t that I was constantly learning and figuring out. That actually was the fun Read more…

Using map, flatMap and compactMap in Combine

Oftentimes when you’re working with Combine, you’ll have publishers that produce a certain output. Sometimes this output is exactly what you need, but often the values that are output by a publisher need to be transformed or manipulated somehow before they are useful to their subscribers. The ability to do this is a huge part of what Combine is, what makes it so powerful, and Functional Reactive Programming (FRP) in general. In this week’s post, I will show you several Read more…

Updating UI with assign(to:on:) in Combine

So far in my series of posts about Combine, we have focussed on processing values and publishing them. In all of these posts, I used the sink method to subscribe to publishers and to handle their results. Today I would like to show you a different kind of built-in subscriber; assign(to:on:). This subscriber is perfect for subscribing to publishers and updating your UI in response to new values. In this post, I will show you how to use this subscriber, Read more…

Publishing property changes in Combine

In Combine, everything is considered a stream of values that are emitted over time. This means that sometimes a publisher can publish many values, and other times it publishes only a single value. And other times it errors and publishes no values at all. When your UI has to respond to changing data, or if you want to update your UI in response to a user’s actions, you might consider the data and user input to both be streams of Read more…

Debugging network traffic with Charles

When you perform a URL Request in your app, you typically configure the request in your code and when it’s all set up you pass it off to a URLSession data task, and the request should succeed if everything goes as expected. When the request is misconfigured, the server will hopefully return a useful error and you can fix your code accordingly. There are times, however, where the server does not give the information you need. Or your requests succeed Read more…

How to sort an Array based on a property of an element in Swift

The easiest way to sort an Array in Swift is to use the sort method. This method is available for all Arrays that have Equatable elements and it sorts your array in place: var words = [“hello”, “world”, “this”, “is”, “a”, “list”, “of”, “strings”] words.sort() // words is now [“a”, “hello”, “is”, “list”, “of”, “strings”, “this”, “world”] This modifies the input Array and sorts its elements using String conformance to Equatable. But what if you want to sort the Array Read more…

Refactoring a networking layer to use Combine

In the past two weeks I have introduced you to Combine and I’ve shown you in detail how Publishers and Subscribers work in Combine. This week I want to take a more practical route and explore Combine in a real-world setting. A while ago, I published a post that explained how you can architect and build a networking layer in Swift without any third-party dependencies. If you haven’t seen that post before, and want to be able to properly follow Read more…