Hacking with Swift News, tips, and tutorials from Hacking with Swift https://www.hackingwithswift.com/articles/rss (c)2020 Paul Hudson https://www.hackingwithswift.com/favicon-96x96.png Hacking with Swift https://www.hackingwithswift.com/articles/rss What's new in Swift 5.8 https://www.hackingwithswift.com/articles/256/whats-new-in-swift-5-8 https://www.hackingwithswift.com/articles/256/whats-new-in-swift-5-8 Back-deployable APIs, more implicit self upgrades, improved result builders, and more! Wed, 08 Mar 2023 10:56:01 +0000 Although many major Swift changes are currently percolating through Swift Evolution, Swift 5.8 itself is more of a clean up release: there are additions, yes, but there are more improvements that refine functionality that was already in widespread use. Hopefully this should make adoption much easier, particularly after the mammoth set of changes that were squeezed into Swift 5.7!

In this article I’m going to walk you through the most important changes this time around, providing code examples and explanations so you can try it all yourself. You’ll need Xcode 14.3 or later to use this, although some changes require a specific compiler flag before Swift 6 finally happens.

Lift all limitations on variables in result builders

SE-0373 relaxes some of the restrictions on variables when used inside result builders, allowing us to write code that would previously have been disallowed by the compiler.

For example, in Swift 5.8 we can use lazy variables directly inside result builders, like so:

struct ContentView: View {
    var body: some View {
        VStack {
            lazy var user = fetchUsername()
            Text("Hello, \(user).")

    func fetchUsername() -> String {

That shows the concept, but doesn’t provide any benefit because the lazy variable is always used – there’s no difference between using lazy var and let in that code. To see where it’s actually useful takes a longer code example, like this one:

// The user is an active subscriber, not an active subscriber, or we don't know their status yet.
enum UserState {
SwiftUI by Example: Now updated for iOS 16 https://www.hackingwithswift.com/articles/255/swiftui-by-example-now-updated-for-ios-16 https://www.hackingwithswift.com/articles/255/swiftui-by-example-now-updated-for-ios-16 100 new Xcode projects to download and try. Thu, 01 Dec 2022 21:41:36 +0000 This week I published the biggest ever update to SwiftUI by Example, adding lots of new sample code plus 100 new Xcode projects to download.

The initial goal was to update the book for iOS 16, but I ended up going back and adding coverage of functionality introduced in iOS 15 and even 14 – this update is just packed with extra code, improved examples, and more.

To save you having to dig through and look for what’s new, here’s a list of the biggest changes:

WWDC22: Wrap up and recommended talks https://www.hackingwithswift.com/articles/254/wwdc22-wrap-up-and-recommended-talks https://www.hackingwithswift.com/articles/254/wwdc22-wrap-up-and-recommended-talks Together again for lots of best practices, backed up with a sprinkling of big new features. Thu, 23 Jun 2022 22:27:27 +0000 This year was a WWDC like we’ve never seen before, with 1000 developers being invited into Apple’s home in Cupertino to share their excitement, meet engineers, and even have tours around Apple Park.

Of course, the conference was also available remotely, with digital lounges returning, labs available for everyone, and lots of community events. In this article I’ll go over how I think the event went, pick out the talks I enjoyed the most and would recommend you watch, highlight some of my favorite community events, and put forward a handful of suggestions for how WWDC23 might look.

Code one, code all

After two years of being fully remote, this year Apple took a new approach of having a single-day event to kick things off, with the rest of the week being fully remote. You might think that would have dissuaded folks from wanting to attend, I can assure you dear reader that it did not: the in-person event was packed, and I lost track of how many people I met who came to town for the week without getting into the Apple special event.

Even from the first time folks agreed to meet up – at a community drinks event in San Jose – you could feel the incredible excitement once again, as if 2+ years of absence never happened. Folks were just happy to be able to meet their friends from around the world again, to chat about the projects they were working on, and catch up on lost time.

Of course, Apple themselves contributed greatly to the excitement by inviting developers to tour their new Developer Center just next to Apple Park and the visitor center.


How to use inner shadows to simulate depth with SwiftUI and Core Motion https://www.hackingwithswift.com/articles/253/how-to-use-inner-shadows-to-simulate-depth-with-swiftui-and-core-motion https://www.hackingwithswift.com/articles/253/how-to-use-inner-shadows-to-simulate-depth-with-swiftui-and-core-motion Tilt your device to move the shadow, as if there’s a light source shining from above. Thu, 23 Jun 2022 16:02:20 +0000 SwiftUI comes with a whole range of advanced effects we can use to customize the way our content is drawn, and from iOS 16 onwards we gain another important option: the ability to create inner shadows. Inner shadows create the illusion that some shape or text is cut out, placing the shadows on the area inside the shape as if an overhead light source were in place.

That alone is nice enough, but with a tiny bit of Core Motion work we can make our shadow move as the user’s device is tilted, as if there really were a fixed light overhead. We can then go even further to add some 3D rotation into the shape – you’ll see how that’s only a small step further.

I think you’ll be surprised how easy this effect is, but please do read to the end before you use this code in your own projects – there are some important tips I want you to be aware of!

Tracking Core Motion

Our first step is to create an observable object that can watch for device motion changes, and send them off to any views that might be interested in it. This can be done using CMMotionManager, which we get through the Core Motion framework, so start by adding an import for that:

import CoreMotion

Now we’re going to create a new MotionManager class that conforms to ObservableObject. This needs to have three properties:

  • The CMMotionManager that is currently monitoring movement. This can be a private constant because it’s only for internal use.
  • The amount of X movement we want to apply.
  • The amount of Y movement we want to apply.

Both those last two should trigger change notifications when they are modified, so we’ll mark them with @Published.

So, add this new class now:

class MotionManager: ObservableObject {
    private let motionManager = CMMotionManager()
    @Published var x = 0.0
    @Published var y = 0.0

In my example code, we want to start reading motion data as soon as this manager is started, so we’ll use the ini...

8 Things I Wish I Knew When I Started Programming https://www.hackingwithswift.com/articles/252/8-things-i-wish-i-knew-when-i-started-programming https://www.hackingwithswift.com/articles/252/8-things-i-wish-i-knew-when-i-started-programming I don’t have a time machine, but you can learn from my mistakes! Fri, 17 Jun 2022 10:28:48 +0000 I've been programming for over 25 years now, and today I love it just as much if not more than when I started. But looking back on me as a kid just starting out, I really had no idea what I was doing, so if I were able to build a time machine today there are eight things I would tell a younger version of myself.

Obviously I don’t have a time machine, but that’s okay because you’re here, and if you’re just starting out with your programming career I can guarantee these things will prove useful to you too.

Before I start, I want to say the most important thing of all: young me was extremely impatient – I always, always wanted to jump ahead onto the next thing, without really taking the time to listen to advice. This caused huge problems in the first 10 years of my career, so if you’re already thinking “I’m bored already – I need to jump over to TikTok to watch some comedy fail videos,” maybe you’re on that same track. Trust me, it doesn’t end well.

Okay, you’re still here, so I guess that’s the first test passed!

So let’s get right into it – you can watch the video below, or keep on scrolling for the text version.

1. Technology will change – don’t get stuck on stuff!

Seriously, no matter how much you love Thing X or language Y, it will evolve or go away entirely. I learned Java and C++ at college, I did PHP, JavaScript, and Visual Basic after graduating, then wrote software for Windows Mobile and Xbox 360, built iPhone apps using Objective-C, and more, and almost all those technologies have evolved beyond recognition or are more or less dead now.

And that’s okay – in fact, it’s kind of how our industry works. But it means I cringe super hard when I see language fanatics who insist JavaScript is the worst, or equally those who think JavaScript can do everything under the sun. There really is a reason there are lots of languages out there, and it isn’t because there are a whole bunch of bored langua...

What’s new in Xcode 14? https://www.hackingwithswift.com/articles/251/whats-new-in-xcode-14 https://www.hackingwithswift.com/articles/251/whats-new-in-xcode-14 Source editing just keeps getting better and better Tue, 14 Jun 2022 22:20:01 +0000 While Xcode 13 added some major features such as Vim mode, version control, DocC, and Xcode Cloud, Xcode 14 opts to return to the basics and polish some core features that make the whole experience faster and smarter to use.

In this article I want to walk through some of the many improvements introduced in Xcode 14, many of which weren’t even mentioned by Apple, as well as a few things that I’m less convinced about. Let’s get straight into it…

Source editor improvements

This year is another bumper year for Xcode’s source editor – it just keeps getting smarter and smarter in ways that I hadn’t even imagined were possible.

As an example, let’s say we had a Player struct such as this one:

struct Player: Identifiable {
    var id = UUID()
    var name: String
    var score = 0

Swift will automatically generate a memberwise initializer for that because it’s a struct, but it’s common to want to customize that initializer. So, Xcode 14 will now autocomplete a memberwise initializer for us – just start typing init inside the struct and it will complete to this:

init(id: UUID = UUID(), name: String, score: Int = 0) {
    self.id = id
    self.name = name
    self.score = score

So, now we might say that the score must always be at least 0:

self.score = max(0, score)

This even works with Codable – if you add a Codable conformance to the struct then start typing encode inside the struct, it will autocomplete this:

func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.id, forKey: .id)
    try container.encode(self.name, forKey: .name)
    try container.encode(self.score, forKey: .score)

This relieves a lot of boilerplate for those times when you want to make one small change to your Codable conformance.

Moving on, we might create an array of users...

What’s new in SwiftUI for iOS 16 https://www.hackingwithswift.com/articles/250/whats-new-in-swiftui-for-ios-16 https://www.hackingwithswift.com/articles/250/whats-new-in-swiftui-for-ios-16 Bottom sheets, fixed grids, and a wholly new way to handle navigation Tue, 07 Jun 2022 09:27:43 +0000 WWDC was back in person for the first time since 2019, and once again was absolutely packed with new features for app developers – huge new features in Swift 5.7, new APIs such as WeatherKit and Swift Charts, and, of course, lots of goodies for SwiftUI developers.

Please keep in mind that these changes are very new – I'm pushing it as hard as I can, experimenting, refining, sharing, and learning all at the same time. If you have any feedback, please tweet me @twostraws. I’ll be adding more soon!

You can watch the video below, or scroll down for links to articles.

The big stuff

Additional: It’s now possible to create multi-column tables on iOS and iPadOS, but I think it’s a bit buggy in beta 1. I’ve documented it here in case you’d like to try it yourself: How to create multi-column lists using Table.

More welcome improvements

What’s new in Swift 5.7 https://www.hackingwithswift.com/articles/249/whats-new-in-swift-5-7 https://www.hackingwithswift.com/articles/249/whats-new-in-swift-5-7 Or as I’ve started calling it, what isn’t new in Swift 5.7? Mon, 06 Jun 2022 16:35:32 +0000 Swift 5.7 introduces another gigantic collection of changes and improvements to the language, including power features such as regular expressions, quality of life improvements like the if let shorthand syntax, and a great deal of consistency clean ups around the any and some keywords.

In this article I want to introduce you to the major changes, providing some hands-on examples along the way so you can see for yourself what’s changing. Many of these changes are complex, and many of them are also interlinked. I’ve done my best to break things down into a sensible order and provide hands-on explanations, but it took a huge amount of work so don’t be surprised to spot mistakes – if you find any, please send me a tweet and I’ll get it fixed!

  • I’m grateful to Holly Borla for taking the time to answer questions from me regarding the new generics proposals – if any mistakes crept through, they are mine and not hers.

  • Tip: You can also download this as an Xcode playground if you want to try the code samples yourself.

if let shorthand for unwrapping optionals

SE-0345 introduces new shorthand syntax for unwrapping optionals into shadowed variables of the same name using if let and guard let. This means we can now write code like this:

var name: String? = "Linda"

if let name {
    print("Hello, \(name)!")

Whereas previously we would have written code more like this:

if let name = name {
    print("Hello, \(name)!")

if let unwrappedName = name {
    print("Hello, \(unwrappedName)!")

This change doesn’t extend to properties inside objects, which means code like this will not work:

struct User {
    var name: String

let user: User? = User(name: "Linda")


Swift Core Team to Swift bloggers: “Please, for the love of all things holy, find a different color than orange” https://www.hackingwithswift.com/articles/248/swift-core-team-to-swift-bloggers-please-for-the-love-of-all-things-holy-find-a-different-color-than-orange https://www.hackingwithswift.com/articles/248/swift-core-team-to-swift-bloggers-please-for-the-love-of-all-things-holy-find-a-different-color-than-orange “Surely some of you can pick a different color? Please?” Fri, 01 Apr 2022 08:52:19 +0000 Early this morning Apple published an unprecedented tech note aimed at Swift bloggers around the world, pleading them with them to be more creative with their color choices.

Ed Kermenek, the Swift project lead, had this to say: “Look, we get it: we chose orange for the logo way back in 2014, and that obviously led some people to think of it as the Official Swift Color or something, but come on – surely some of you can pick a different color? Please?”

Taking a break from his work on Swift Enterprise Edition, Jay Gorff took a more hardline stance: “Frankly, I never want to see the color orange again. I can’t eat oranges, carrots, or pumpkins any more, and you jerks have completely ruined Cheetos for me too. Wait… it’s almost sunset, and I can already feel my stomach getting into a knot thinking of the colors – do you see what you monsters have done?!”

Bolly Horla, who is somehow still not on the Swift Core Team despite being more than qualified, pulled out a tabbed binder of colors the team considered more appropriate. “How about a nice blue,” she said, “or some shade of chartreuse? Ooh, why not try plaid? I bet we could solve this with some kind of @Color property wrapper…”

Steve Prestoff, currently working to make Swift compile faster, took a break to add: “Chickens.” We’re not sure if that means he likes the color of chickens, the taste of chickens, or something else, but at least he’s not on a hor— oh, wait, he’s back on a horse again.

What’s new in Swift 5.6? https://www.hackingwithswift.com/articles/247/whats-new-in-swift-5-6 https://www.hackingwithswift.com/articles/247/whats-new-in-swift-5-6 Type placeholders, unavailable checks, Codable improvements, and more. Wed, 02 Mar 2022 14:37:06 +0000 Swift 5.6 introduces another barrage of new features to the language, while refining others as we get closer to Swift 6. In this article I want to introduce you to the major changes, providing some hands-on examples along the way so you can see for yourself what’s changing.

Introduce existential any

SE-0335 introduces a new any keyword to mark existential types, and although that might sound esoteric please don’t skip ahead: this one is a big change, and is likely to break a lot of Swift code in future versions.

Protocols allow us to specify a set of requirements that conforming types must adhere to, such as methods they must implement. So, we often write code like this:

protocol Vehicle {
    func travel(to destination: String)

struct Car: Vehicle {
    func travel(to destination: String) {
        print("I'm driving to \(destination)")

let vehicle = Car()
vehicle.travel(to: "London")

It’s also possible to use protocols as generic type constraints in functions, meaning that we write code that can work with any kind of data that conforms to a particular protocol. For example, this will work with any kind of type that conforms to Vehicle:

func travel<T: Vehicle>(to destinations: [String], using vehicle: T) {
    for destination in destinations {
        vehicle.travel(to: destination)

travel(to: ["London", "Amarillo"], using: vehicle)

When that code compiles, Swift can see we’re calling travel() with a Car instance and so it is able to create optimized code to call the travel() function directly – a process known as static dispatch.

All this matters because there is a second way to use protocols, and ...

Special Effects with SwiftUI https://www.hackingwithswift.com/articles/246/special-effects-with-swiftui https://www.hackingwithswift.com/articles/246/special-effects-with-swiftui TimelineView, Canvas, particles, and… AirPods?! Wed, 26 Jan 2022 16:30:52 +0000 Everyone knows SwiftUI does a great job of creating standard system layouts with lists, buttons, navigation views, and more, and while it’s really useful I doubt most people would call it fun.

So, in this article I’m going to show you how to use SwiftUI to build something fun, beautiful, and unlike anything you’ve seen before. You’ll need at least Xcode 13 and iOS 15 or later, but you also need to download a single image from my site from here: https://hws.dev/spark.zip.

Getting started

At the core of our little experiment is a particle system, which is commonly used in games to create effects like fire, smoke, rain, and more. We’re going to start simple and work our way up – it’s pretty amazing how fast we can move with SwiftUI.

The first step is easy: create a new iOS project using the App template, making sure to choose SwiftUI for your interface. You should already have downloaded the spark.zip file from my site, which contains a single image. I’d like you to drag that into your project’s asset catalog, so we have an image to use for all our particles.

Next, I’d like to think about what it means to store one particle in a particle system – what does one rain drop need to store, or one snowflake? There are all sorts of values we could store, but we need only three: the X and Y coordinates for the particle, plus the date it was created.

In addition to that, I’m also going to add a conformance to the Hashable protocol, so we can add our particles to a set and remove them easily.

Create a new Swift file called Particle.swift, and give it this code:

struct Particle: Hashable {
    let x: Double
    let y: Double
    let creationDate = Date.now.timeIntervalSinceReferenceDate

So, that stores a single particle in our particle system – that’s one drop of rain, one spark from a fire, one piece of fairy dust, or whatever kind of particles you’re working with.

One level up from that is the pa...

How to build your first SwiftUI app with Swift Playgrounds https://www.hackingwithswift.com/articles/245/build-your-first-swiftui-app-with-swift-playgrounds https://www.hackingwithswift.com/articles/245/build-your-first-swiftui-app-with-swift-playgrounds Learn the basics of SwiftUI and build a real app along the way. Wed, 15 Dec 2021 22:25:43 +0000 Swift Playgrounds lets you build a complete iOS app from scratch, right on your iPad. You get to draw upon Apple’s powerful SwiftUI framework, you can pull in libraries that other people have written, and if you want you can even move your project over to your Mac and continue it in Xcode, Apple’s full-blown code editor.

In this tutorial I’m going to walk you through building your very first app using Swift Playgrounds for iPad, where you’ll be able to show pictures of your friends and family, and tapping one of them will play a sound – it’s just for fun, but you’ll learn a lot along the way, and also make use of other built-in software on your iPad. This tutorial is aimed at beginners, so you don’t need any prior Swift experience.

To follow along you’ll need to install Playgrounds from the App Store, but that’s it – you don’t need Xcode or even a Mac. In case you were curious, I’m using Apple’s Magic Keyboard with an iPad Pro, running iOS 15.2.

First steps

To get started, press the “App” button under “Get a Playground” to create a new project. This will be called My App by default, and it will be given a random icon – I’ll show you how to change those later.

Go ahead and tap your new playground to open it for editing, and you’ll see Playgrounds splits into two parts: on the left is some simple Swift code to get us started, and on the right is a live preview that shows your actual code running.

I expect the starter code will change over time, but that’s okay because I want you to delete almost all of it – leave only this behind:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")

As you delete parts, you’ll see the preview on the right change immediately – it really is live, and later on you’ll see it’s interactive too.

Our remaining code does several things:

  1. It tells Swift to bring in the SwiftUI fra...
Swift Playgrounds 4 is here, and it’s a thing of beauty https://www.hackingwithswift.com/articles/244/swift-playgrounds-4 https://www.hackingwithswift.com/articles/244/swift-playgrounds-4 Building complete apps on your iPad is now possible. Tue, 14 Dec 2021 16:41:59 +0000 At WWDC21 Apple revealed that Swift Playgrounds was getting a huge upgrade to allow us to build complete SwiftUI apps, including not only a live preview of our work but also the ability to ship direct to the App Store.

Well, Playgrounds 4 just arrived and I’m really pleased to say they nailed it: this is a landmark release for Swift Playgrounds, introducing instant interactive previews, lightning-fast code completion, complete integration for Swift Package Manager repositories, and so much more…

In case you’re short on time, here’s the TL;DR:

  • Yes, you can now build SwiftUI apps right on your iPad.
  • Yes, you can use also UIKit if you prefer.
  • Yes, you can ship your apps to the App Store straight from your iPad.
  • Yes, it supports Swift Package Manager packages from GitHub.
  • Yes, you get the fantastic new code completion, just like with Xcode.
  • Yes, it ships with Swift 5.5 and all the concurrency awesomeness that entails.

Okay, let's explore the app…

Building iPad apps on your iPad

Folks have been requesting Xcode for iPad for some time, but that would have required a pretty epic effort – does that mean all of Interface Builder? All the Objective-C and C++ support? Or – *cue silent screaming* – Info.plist files?

Swift Playgrounds has chosen a different way: rather than trying to recreate all of Xcode on iPadOS, it instead aims to produce “Diet Xcode” – by which I mean “slimmer, faster, and streamlined” and not “why does my drink taste weird.” That means we get Xcode-style code completion that appears instantly, we get Xcode-style instant SwiftUI previews as we type, we get Xcode-style imports for SPM packages through Git, and much more.

![Swift Playgrounds 4 showing inline code completion.](/uploads/arti...

Write better code with Swift Algorithms https://www.hackingwithswift.com/articles/243/write-better-code-with-swift-algorithms https://www.hackingwithswift.com/articles/243/write-better-code-with-swift-algorithms Write faster, simpler, safer Swift code with this powerful open-source package. Thu, 02 Dec 2021 22:55:29 +0000 Swift’s standard library is packed with types and functions to solve the most common code problems quickly and efficiently, but it doesn’t cover everything. So, for more advanced needs we need to turn to Swift Algorithms: Apple’s open source package of sequence and collection algorithms, tuned for maximum performance and flexibility.

With the 2021 Advent of Code happening right now, this is a great time to see how Swift Algorithms can help you write faster, simpler, and safer code. There’s functionality for uniquing sequences, chunking them, selecting several random elements, compacting them, and more, and most return new, highly-optimized sequence types that are more efficient than flattening everything to a simple array.

Plus, Apple has said that Swift Algorithms provides an opportunity to explore algorithmic problems and solutions before graduating them into the main standard library – you get better code today, and a look in at what the standard library might become in the future. What’s not to like?

Best of all, adding Swift Algorithms to your Xcode project takes only a few moments: go to the File menu, choose Add Packages, select Apple Swift Packages, then select “swift-algorithms” and click Add Package. Now just add import Algorithms to your code, and you’re all set!

In this article I’m going to pick out just a little of what Swift Algorithms can already do, focusing on nine particular algorithms I find most useful. Let’s get to it…

  • Prefer to watch this as a video? Here you go!

Chaining sequences

If you have two arrays of data and want to loop over them both, you might write something like this:

let names1 = ["Jane", "Elizabeth", "Mary", "Kitty"]
let names2 = ["Daphne", "Eloise", "Francesca", "Hyacinth"]

for name in names1 + names2 {

That will print all eight names, but in...

Learn essential Swift in one hour https://www.hackingwithswift.com/articles/242/learn-essential-swift-in-one-hour https://www.hackingwithswift.com/articles/242/learn-essential-swift-in-one-hour Here’s the least you need to know to get started with Swift. Fri, 15 Oct 2021 21:23:01 +0000 In this article I’m going to introduce you to the fundamentals of the Swift programming language in about one hour.

This article is aimed at two kinds of people: folks who have completed the introduction to my 100 Days of SwiftUI course and are looking for a quick review, and folks who are experienced with other languages and want to transfer their skills over to Swift.

We’re going to move fast because this is designed to be a primer – if you find yourself struggling to understand something, visit the 100 Days of SwiftUI to complete the longer, more detailed introduction there.

Let’s get to it!

Creating constants and variables

Swift can create constants and variables, but constants are generally preferable.

Use this to create then change a variable string:

var name = "Ted"
name = "Rebecca"

If you don’t want to change a value, use a constant instead:

let user = "Daphne"

The print() function is helpful for learning and debugging, and shows some information about a variable:



Swift’s strings start and end with double quotes:

let actor = "Tom Cruise"

And they work great with emoji too:

let actor = "Tom Cruise 🏃‍♂️"

If you want double quotes inside your string, place a backslash before them:

let quote = "He tapped a sign saying \"Believe\" and walked away."

If you want a string that spans multiple lines, start and end with three double quotes, like this:

let movie = """
A day in
the life of an
Apple engineer

Swift provides many useful properties and methods for strings, including .count to read how many letters it has:


There are also hasPrefix() and hasSuffix(), which lets us know whether a string starts or ends with specific letters:


Important: Strings are case-sensitive in Swift, so ...

My favorite new Swift API from iOS 15 https://www.hackingwithswift.com/articles/241/how-to-fetch-remote-data-the-easy-way-with-url-lines https://www.hackingwithswift.com/articles/241/how-to-fetch-remote-data-the-easy-way-with-url-lines AsyncSequence and effectful read-only properties combine to make something beautiful. Thu, 19 Aug 2021 13:11:29 +0000 WWDC21 came packed with lots of new features for Swift, SwiftUI, Foundation, and more, but in all the WWDC videos I’ve watched only one made me do a double take – I had to rewind and rewatch it just to make sure I hadn’t misheard.

The feature sounds simple, but makes for quite astonishing code: it’s the lines property on URL, which downloads and returns lines of text from a URL as they are fetched. Internally this builds on a huge amount of functionality, not least effectful read-only properties and AsyncSequence, and the end result is quite remarkable in terms of its simplicity.

In its simplest form this means we can access string data directly from a URL, either local or remote. This is perfect for simpler APIs that vend data line by line, such as plain-text files or CSV – Swift even keeps the connection open as long as necessary so new data can be streamed in as needed.

Trying it out with SwiftUI

Using this API, we could write a small app to fetch a collection of quotes from a server and display them in a list, all using hardly any code:

struct ContentView: View {
    @State private var quotes = [String]()

    var body: some View {
        List(quotes, id: \.self, rowContent: Text.init)
            .task {
                do {
                    let url = URL(string: "https://hws.dev/quotes.txt")!

                    for try await quote in url.lines {
                } catch {
                    // Stop adding quotes when an error is thrown

Perhaps you can see what made me do the double take in the first place – I was surprised to be able to access lines directly on a URL, rather than routin...

Hacking with Swift Live 2021 raises $61,000 for charity https://www.hackingwithswift.com/articles/240/hacking-with-swift-live-2021-raises-61000-for-charity https://www.hackingwithswift.com/articles/240/hacking-with-swift-live-2021-raises-61000-for-charity Make apps, make friends, make a difference. Thu, 05 Aug 2021 14:42:37 +0000 Hacking with Swift Live 2021 just finished, delivering three days of tutorials and workshops on all the major concurrency changes to Swift, the many improvements for SwiftUI, and more. Even better, because all our profits go to charity we were able to donate $61,000 to an incredible cause – everyone got to take their Swift skills forward, meet new friends, and make a real difference outside our community.

Just like last year, we donated all our profits to Special Effect, who help to bring joy, creativity, and confidence into the lives of people with disabilities. With the pandemic somehow still raging many charities continue to have a hard time raising funds, so we’re glad to play a part in helping continue their important work.

The feedback so far has been overwhelmingly positive, and I’m really grateful to everyone to bought a ticket, our individual sponsors, the many folks who bought diversity tickets, and of course our corporate sponsors.

This year we were lucky enough to have two platinum sponsors:

  • DuckDuckGo has a private search engine, a privacy-focused browser app, and a privacy browser extension, so I think you know exactly what their #1 priority is!
  • Instabug’s tools can help you find and fix bugs faster thanks to comprehensive crash reporting and performance metrics.

We also had three silver sponsors:

  • Backbase are building a software platform to help banks move faster in a digital-first world.
  • Emerge analyses your code to provide insights to help make your app size smaller, and even generates size diffs on every PR!
  • Bitrise offers fast, flexible, and scalable continuous integration and delivery for app developers, offering dozens of integrations with well-known tools.

Each of those sponsors went out of their way to support our event, not only making significant charity donations but al...

WWDC21: Wrap up and recommended talks https://www.hackingwithswift.com/articles/239/wwdc21-wrap-up-and-recommended-talks https://www.hackingwithswift.com/articles/239/wwdc21-wrap-up-and-recommended-talks Packed sessions, digital lounges, and a look forward to 2022. Sun, 20 Jun 2021 21:39:38 +0000 Apple’s annual event is done and dusted for another 12 months, featuring a major raft of updates for all platforms, plus Swift, SwiftUI, UIKit, and more. But perhaps just as importantly, this year also featured a number of improvements to the conference format itself, and it seems increasingly clear that we cannot go back to the old WWDC format again.

In this article I’ll go over how I thought the event went, pick out the talks I enjoyed the most and would recommend you watch, highlight some of my favorite community events, and put forward a handful of suggestions for how WWDC22 might look.

And away we code!

WWDC20 took place during a global pandemic, and WWDC21 also took place during a global pandemic. But this year Apple was prepared – perhaps they had some plans in place for an in-person WWDC just in case things had improved by the start of the year, but it seems clear this year a great deal of work had gone into making WWDC work better as an event for everyone.

Even from the very first keynote video it was obvious this year Apple had doubled down on the production quality for their videos. Yes, Craig’s teleportation effect somewhat stole the show, but also we saw carefully orchestrated presenter handovers across a wide team and a variety of impressive CGI overlays that brought the launch day to life.

A woman talking in front of the Swift logo. This is Susan Prescott, introducing a segment on developer technologies.

But the keynote did a lot to set the stage for the week. Yes, Craig called developers “the heart and soul of WWDC” – I mean, it’s right there in the name – but things really kicked off when Ted Kremenek took over presenting. I can’t imagine I was alone in feeling my pulse pick up when that happened, because Apple only shows code during the keynote when they mean business. Unsurprisingly this segment focused on concurrency, and even if you’ve only managed to watch a few of the videos from WWDC21 you’ll almost ...

How to document your project with DocC https://www.hackingwithswift.com/articles/238/how-to-document-your-project-with-docc https://www.hackingwithswift.com/articles/238/how-to-document-your-project-with-docc Markdown-powered documentation is now just a click away. Sun, 13 Jun 2021 15:22:35 +0000 DocC is Apple’s new tool for building beautiful, interactive, and native-feeling documentation right from your code, and it integrates smoothly into both Xcode and the web.

In this article I’m going to walk you through the fundamentals of using it in two different ways: building documentation for a framework, and adding articles that provide more depth. Along the way, you’ll see some places where DocC works brilliantly, and also some places where it doesn’t really work like I’d expect at all. And that’s okay – it’s only the initial release, after all.

Before I start, though, there are some basics:

  • You need to have Xcode 13 installed in order to have access to DocC.
  • It supports only Swift at this time. They have already received requests to add Objective-C support, and in the long term I hope they’ll add support for other languages too.
  • In case you were curious, DocC is short for “Documentation Compiler”, and it’s pronounced with two equally stressed syllables – “Doc-See” as opposed to “doxy”.
  • Xcode is packed with lots of major new features – it’s another real stand out year, and I hope the team are feeling really proud. You can find out more in my article What’s new in Xcode 13.

Please keep in mind I’m using Xcode 13 beta 1, so it’s all early days at this point – I expect DocC to get polished up a lot before the final release later this year.

First, what DoC is not

The first thing I did when trying out DocC is what I expect most app developers would do: I opened up one of my projects to see what DocC would make of it. In my case, that was Unwrap – it’s a good size of project with various dependencies, and matches the kind of thing many app developers will be working with.

If you want to try this in your own app project, just go to the Product menu and choose Build Documentation. It might take a minute or two to ...

The Complete Guide to SF Symbols https://www.hackingwithswift.com/articles/237/complete-guide-to-sf-symbols https://www.hackingwithswift.com/articles/237/complete-guide-to-sf-symbols Example code, tips, and techniques for both SwiftUI and UIKit Sun, 13 Jun 2021 08:06:10 +0000 SF Symbols allows us to render a huge and growing variety of icons inside our apps, and every year it gets bigger – not only do many more icons get added, but iOS 14 added the ability to render multicolor icons and iOS 15 added the ability to have complete control over individual layers.

In this article I’m going to walk you how to use SF Symbols in your projects, providing code for both SwiftUI and UIKit. Let’s get into it…

Tip: The best way to browse the list of symbols is using Apple’s free SF Symbols app.

How to load an SF Symbol

The simplest thing you’re going to want to do is load a symbol. We’ll be using “star” here, but if you’re using the SF Symbols app you can right-click any symbol and choose Copy Name.

In SwiftUI loading an image is done by placing an Image into your view hierarchy using its systemName initializer:

Image(systemName: "star")

In UIKit you need to use UIImage then place it in a UIImageView, like this:

let image = UIImage(systemName: "star")
let imageView = UIImageView(image: image)

How to place SF Symbols next to text

SF Symbols work great when used inside text, particularly when they contain common symbols such as error crosses, check marks, calendars, and similar.

In SwiftUI placing an image next to text is done like this:

Label("Please try again", systemImage: "xmark.circle")

If you want to place it inside the text string, it’s better to use string interpolation with Image, like this:

Text("Please press the \(Image(systemName: "calendar")) button")

For UIKit the code is a little more complex because you need to use NSAttributedString and NSTextAttachment to render something into a UILabel, but the same code lets you place the images wherever you want them:

let attachment = NSTextAttachment()
attachment.image = UIImage(systemName: "xmark.circle")

let imageString = NSMutableAttributedString(attachment: attach...