Downloading files from URLs in Swift [SwiftUI Architecture]

Downloading files in Swift can be pretty straightforward, thanks to URLSession and URLSessionDownloadTask.

Moreover, tracking the progress of a download can be easily achieved with URLSessionDownloadDelegate.

However, SwiftUI apps often need to track the download progress of multiple files, complicating their networking architecture.

URLSession in Swift: The Essential Guide [with Examples]

When you need to download or upload data in Swift, the URLSession class is the solution you should reach for.

URLSession handles the most common Internet protocols and allows you to:

  • Group and optimize data transfers.
  • Handle authentication, cookies, and caching.
  • Pause, resume, or cancel a download.
  • Download data in the background when your app is suspended.

AsyncStream and AsyncSequence for Swift Concurrency

Asynchronous sequences are a core feature of Swift concurrency. They allow you to process asynchronous events easily using simple for loops.

However, conforming to the AsyncSequence protocol to create an asynchronous sequence is not straightforward.

That’s where AsyncStream comes to the rescue, allowing you to easily create asynchronous sequences and quickly adapt old APIs to Swift concurrency.

AsyncImage in SwiftUI: Loading Images from URLs [with Caching]

AsyncImage is a convenient SwiftUI view that loads remote images using a URL. It’s especially useful in apps that interact with REST APIs since images are usually referenced using URLs in JSON data.

While Apple’s documentation might lead you to believe that AsyncImage caches the downloaded images, that is not the case. If you need image caching, you must implement your own custom solution.

Parsing JSON in Swift: The Complete Guide [With Examples]

Parsing JSON data is fundamental to any iOS app that performs remote REST API calls.

Thanks to the Codable protocols introduced in Swift 4, Swift has a native and idiomatic way to parse JSON data.

Paired with the JSONDecoder class, the Decodable protocol allows straightforward JSON decoding in a few lines of code and more sophisticated techniques to handle all the possible data formats and edge cases.

Decoding JSON with Dynamic Types in Swift [With Codable]

JSON data is not always homogenous. There are instances in which a JSON object field can contain objects containing different information.

In such cases, the Swift type for the decoding must be determined dynamically.

Decode JSON with Dynamic Keys in Swift [Dictionaries and Arrays]

JSON object with dynamic keys presents a specific challenge when decoding data in Swift because stored properties cannot represent dynamic keys.

However, it’s still possible to decode JSON with dynamic keys using the Decodable protocol into dictionaries or arrays by implementing structures that conform to the CodingKey protocol.