What’s new in Swift 6.1?
You’re more of a video kind of person? I’ve got you covered! Here’s a video with the same content than this article 🍿
Advertisement
Easy, Secure In-Person Payments for Mobile Apps
Square’s NEW Mobile Payments SDK:
PCI-compliant, embedded payments
Supports diverse hardware for in-person payments
Reliable on the move, over the counter, & at a kiosk
Free to integrate and no API usage costs
Sponsors like Square really help me grow my content creation, so if you have time please make sure to check out their survey: it’s a direct support to my content creation ☺️
If you’ve updated to Xcode 16.3, you’re already using Swift 6.1!
So how about we go over some of its new features?
If you use Swift Concurrency, and more specifically TaskGroup
, you’ll be happy to hear that the syntax to create a TaskGroup
has been simplified.
Until now, you had to explicitly provide the return type of the child tasks:
This always felt a bit weird and cumbersome, because we’re so used to the Swift compiler being able to infer such types automatically.
But here the closure is more complex than usual, too complex for the compiler to infer the type.
This is however no longer the case, because in Swift 6.1 the compiler can finally infer the return type of the child tasks:
For experienced developers, this will make writing concurrent code a tiny bit nicer and for beginners it will likely make TaskGroup
easier to grasp!
Next, if like me you like splitting your code on several lines as much as possible, you might already be using trailing comas:
The idea is very simple: you add a coma after the last element of a list, even though there’s no element coming after it.
This might sound weird, but it’s quite useful: if you need to add a new value later, you won’t have to modify an existing line just to add a coma.
This helps making code reviews more focused on meaningful changes and also to have the git author
command yield more relevant results.
In Swift 6.1 you can now use trailing comas basically everywhere you need to provide a list of values, like, for example, in an argument list:
Finally, if you’re already using Swift Testing you’ll be happy to learn that the framework has been improved to better handle the set up and tear down logic of your tests.
Back when Swift Testing was released, Apple’s official recommendation to handle such logic was to use the init
and the deinit
of the type enclosing your tests.
This worked, but it didn’t felt like the most elegant of solutions, because sharing that logic between several types felt clunky and error prone.
Apple must have come to the same conclusion because they’ve added a Trait
mechanism that allows you to define your set up and tear down logic once, and then easily share it across several tests:
That’s it, these were the three new features of Swift 6.1 that I wanted to show you!
This list wasn’t exhaustive however: Swift 6.1 contains a few more new features: you can learn about them by reading the official release note.