Automatically retrying an asynchronous Swift Task
A few examples on how to use Swift Concurrency to write asynchronous operations that are automatically retried if an error was encountered.
Articles, podcasts and news about Swift development, by John Sundell.
Frequently published articles about Swift techniques, language features, architectual patterns, and beyond.
Subscribe via RSS
A few examples on how to use Swift Concurrency to write asynchronous operations that are automatically retried if an error was encountered.
How to use either explicit self references or a capture list to capture self strongly within an escaping Swift closure.
How a custom SwiftUI button that’s capable of running asynchronous actions can be created, and how to make such a control versatile and easy to reuse across a project.
How we can use the built-in Task type to delay certain operations when using Swift’s new concurrency system.
What’s the difference between those two ways, and when should which be used?
How to make it possible to use async system APIs on earlier operating system versions.
Starting in Xcode 13.2, Swift’s new suite of concurrency features are now backward compatible all the way back to iOS 13, macOS Catalina, watchOS 6, and tvOS 13.
Let’s take a look at which of the new SwiftUI APIs that are backward compatible with earlier OS versions, and what language features that made that possible.
How to preview a SwiftUI view in landscape using Xcode 13, and how to enable that new feature to be used within projects that also support previous iOS versions.
How the share operator enables a Combine publisher to be reused without requiring duplicate work to be performed for each subscriber.
How Codable’s auto-synthesis works for enums, and how that part of the system has been upgraded in Swift 5.5 to support associated values.
How Swift 5.5 enables us to conditionally compile postfix member expressions using compiler directives, and what kinds of situations that this new feature could be useful in.
How we can now use Swift’s very convenient “dot syntax” to refer to protocol-conforming types, and how that improves some of SwiftUI’s styling APIs.
What Swift’s @unknown attribute does, and why the compiler tells us to use it when switching on certain enums.
How Swift 5.5 enables computed properties to become either throwing or asynchronous, and what sort of situations that these new capabilities could become useful in.
New in Swift 5.5: Property wrappers can now be applied directly to function arguments, just like how they can be used to add additional functionality to a property or local variable.
How to compose multiple SwiftUI views to render a shape that’s both stroked and filled at the same time.
Three different ways to dismiss a SwiftUI view that was presented from another view, including a new API that’s being introduced in iOS 15.
Various ways of conditionally creating SwiftUI views that depend on optional values.
Let’s explore how we can make it possible to call async/await-powered APIs within a Combine pipeline.
How the Swift compiler is now able to automatically perform conversions between Double and CGFloat values.
Great options for when we don’t want to create a whole new Xcode project just to try out a quick UI idea.
It’s now possible to create a local variable that’s assigned to an expression that has the same name, without having to manually disambiguate with ‘self’.
How the frame modifier can be used to create resizable views that fill the container they’re rendered in.
Starting in Swift 5.4, result builder attributes can now be attached directly to closure-based properties. Let’s take a look at how that new feature can be used.
How the compiler can automatically map default property values to property wrapper initializers, even when a wrapper accepts multiple arguments.
How implicit member expression can now be chained, and what sort of APIs that this new feature might enable us to design.
How event handling logic can be neatly encapsulated using private methods.
How to round the corners of a UIKit or SwiftUI view in various ways.
How to validate email addresses in Swift using a dedicated RawRepresentable type and Foundation’s NSDataDetector API.
How key paths can be made much more powerful when used to query and filter collections.
How a SwiftUI view’s body can be split up into multiple computed properties, which is a great alternative to using separate types in certain situations.
What’s new in the next major version of Swift by Sundell, and what my plans for 2021 are.
Lightweight state observations.
A neat technique for values that can’t be created with a single line of code.
Using a dedicated protocol to make it easy to define new containers.
Combining dynamic and constant output values.
How Swift 5.3 lets us combine lazy properties with willSet and didSet observers.
Why rewrite views when we can reuse them?
A new addition to UIKit in iOS 14.
A tip for more dynamic navigation.
With or without storyboards.
And how that can be customized.
A few features that make Swift’s multiline string literals really useful.
Really useful for utility methods.
Useful for both apps and widgets.
Specializing container views.
A nice new feature that’s especially useful within SwiftUI views.
Another useful interoperability API.
How SwiftUI’s new StateObject property wrapper works, and how it compares to ObservedObject.
Testable code without any protocols.
Making it easy to observe a single value in a read-only fashion.
Adding more contextual information to test failures.
Celebrating this website’s three-year anniversary.
Sometimes, we’re able to omit certain generic type constraints.
Defining Swift enums based on completely custom types.
Dynamic properties with type safety.
How to observe iOS 13.4’s new granular keyboard events.
Making wrapping views a lot easier.
Different ways of helping the compiler type-check generic code.
Implementing a property wrapper to enable certain properties to be overridden only in debug mode.
A look at the standard library’s Result type, what kind of situations that it might be useful in, and tips and tricks that can be good to keep in mind when working with it.
How Swift’s memberwise initializers work, and when those initializers can be used.
How the standard library enables us to add randomness to our code.
Transforming strings, integers, and other raw values into SwiftUI views.
A few tips on deciding how to implement each given iteration.
Making extensions on generic types more compact.
How enums work in Swift, a look at some of their most prominent features, and examples of situations in which they can be incredibly useful.
In Swift, it’s possible to satisfy a throwing function protocol requirement using a non-throwing function, which can be very useful in certain situations.
How to enable a type to be expressed by strings that contain interpolated values.
A look at a few different ways that closures can be defined and used in Swift, the flexibility of Swift’s closure syntax, and how behaviors like escaping and capturing may impact our code.
How to enable and use test coverage in Xcode.
A recap of the basics of SwiftUI, Apple’s declarative new UI framework for building apps across all of their platforms. How to build custom views and modify existing ones, how to manage local state within a view, and how to compose multiple views to form new ones.
One way of transforming values in Swift is by mapping a collection into an array of new values, using a transform. The Swift standard library offers three main APIs for that kind of mapping — map, flatMap and compactMap. Let’s take a look at how they work.
Swift enables us to create generic types, protocols, and functions, that aren’t tied to any specific concrete type — but can instead be used with any type that meets a given set of requirements. Let’s take a look at how generics work in Swift, and how to create our own.
Just like modern versions of Objective-C, Swift uses the ARC (Automatic Reference Counting) memory management model. Let’s take a look at how ARC works, and how to avoid common memory-related issues, such as retain cycles.
How an application deals with errors and unexpected values is arguably just as important as how it deals with valid results. Let’s take a look at a few key techniques that can help us provide a better user experience whenever an error was encountered within our code.
Adding animations to an app can be a great way to both delight users, and to draw their attention to certain information or actions, through motion. When deployed in the right places, animations can really make an app appear more polished and easy to use. Let’s take a look at how to get started.
Introduced in Swift 4, the Codable API enables us to leverage the compiler in order to generate much of the code needed to encode and decode data to/from a serialized format, like JSON. Let’s take a look at how to use it.
Most apps these days need a way to download data from the Internet — whether that’s files, images, or by talking to some form of web API through a format like JSON. Let’s take a look at how to do that using Foundation’s URLSession API.
Using unit tests, and other forms of automated testing, can be a great way to protect a code base against regressions and reduce the need for manual testing. Let’s take a look at how to get started.
Just like how a UIView can be added to another UIView to form a hierarchy, a view controller can become the child of another view controller. Let’s take a look at how to do that.
Grand Central Dispatch is one of the few different options available to us when it comes to asynchronous programming in Swift. Let’s take a look at how it works and how to use it.
Auto Layout has undergone quite a lot of changes and improvements over the years, in particular with the introduction of layout anchors in iOS 9. Let’s take a look at how they work.
Let’s take a look at how the Swift 4.2 compiler is able to automatically synthesize conditional conformances to the Equatable and Hashable protocols.
In today's WWDC Update, let's take a look at how Core Animation this year has been made a bit more "Swifty", thanks to some better annotations and how many of its APIs that previously relied on strings now use synthesized enums and option sets.
In this first Daily WWDC Update we’ll try out an exciting new cross-platform framework called Natural Language. It provides a high-level API enabling easy access to a suite of language detection features when working with text in an app.
Assertions are not only an essential tool when writing tests - they're also super useful in order to write more predictable and easier to debug code. This week, let's look deeper into assertions, how they work, and how we can implement our own assert functions for performing various checks.
We often encounter situations in which we need to find a way to store objects based on some concept of identity. Whether it's in a cache, storing representations of objects on disk, or simply using a dictionary - we often need to find ways to uniquely identify the objects that we deal with. Let's take a look at a few such ways.
Swift’s @autoclosure attribute enables us to define an argument that automatically gets wrapped in a closure. It’s primarily used to defer execution of a (potentially expensive) expression to when it’s actually needed, rather than doing it directly when the argument is passed.
Lazy properties allow you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process. This can be useful in order to avoid optionals, or to improve performance when certain properties might be expensive to create. This week, let’s take a look at a few ways to define lazy properties in Swift, and how different techniques are useful in different situations.
A lot of code that we write relies on the current date in some way. Let's take a look at how to test such code in a fast and predictable way, without having to invent a lot of infrastructure or resort to hacky solutions like swizzling the system date.
While Swift does not yet feature a dedicated namespace keyword, it does support nesting types within others. Let’s take a look at how using such nested types can help us improve the structure of our code.
Most apps written for any of Apple’s platforms rely on APIs that are singleton-based. From UIScreen to UIApplication to NSDate, static APIs are everywhere in Foundation, UIKit and AppKit. In this article I'll go through 3 steps to easily test code that uses such APIs.
Dependency injection is a great technique for decoupling code and making it easier to test. In this article, let’s take a look at how functions can be used for simple dependency injection.
How to use custom, lazily evaluated sequences in Swift to improve performance and enable you to deal with larger datasets, such as entries in a database.
How type erasure can be implemented using closures, and how it can help you work around using protocols with self or associated type requirements.
I’d like to share a technique that I’ve come to find quite useful when using Swift’s do, try, catch error handling model — to limit the amount of errors that can be thrown from a given API call.