Articles, podcasts and news about Swift development, by John Sundell.

The Standard Library

Learn how to make great use of Swift’s standard library and the collections, algorithms, types and functionality that it ships with.

Most programming languages ship with some form of standard library that includes built-in, commonly used pieces of functionality, and Swift is no exception. However, Swift takes the concept of a standard library even further, since much of the functionality that we’d perhaps consider to be features baked into the language itself are actually implemented within the standard library.

On this Discover page, you’ll find articles, podcast episodes, tips and tricks that can help you make the most of many of the features and APIs that the standard library offers — which in turn can often help us improve the speed, quality and compatibility of the code that we write.

Collections

Let’s start by taking a look at Swift’s various collection types, and how to perform tasks like sorting, querying, slicing, and transforming elements. We’ll also explore how we can create our very own, custom collections as well.

Strings

Although strings are also technically collections in Swift, they definitely deserve a section of their own. The following articles will give you a thorough look at Swift’s String type and its various APIs, features, and quirks:

Codable

The built-in Codable API lets us encode and decode our Swift types to and from various data formats, such as JSON. The fact that Codable integrates with the Swift compiler itself also means that our implementations of it can often be automatically generated by the compiler — but there are still many different situations in which we might want to tweak things on our own as well. Let’s take a look at how that can be done:

Next, a quick message from this week’s sponsor

Swift by Sundell is completely free for everyone, but if you’d like to support my work, then please take just a few minutes to check out the following sponsor, since doing so really helps me out financially.

Architecting SwiftUI apps with MVC and MVVM

Architecting SwiftUI apps with MVC and MVVM: Although you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

Result

Another incredibly convenient type that ships as part of the standard library is Result, which lets us model the result of an operation as either a success or a failure. Although defining a custom result type takes less than 10 lines of code, the power of having such a type built into the standard library (besides the convenience aspects) is that it lets us use that type as a “bridge” between different APIs, frameworks and code bases — since they can all use the same, default type to model their results.

Errors

The standard library also ships with a dedicated Error protocol that plays a key part in Swift’s standard error handling system — and making full use of that protocol, and the way it lets us propagate and handle errors, can really make an app feel much more polished and user-friendly.

Podcast conversations about the standard library

The Swift standard library was a key topic on the following podcast episodes, which also contain discussions on how we can write our own code in ways that match the design and overall concepts of the standard library itself:

Other tips and tricks

Finally, let’s wrap up this Discover page with a few tips on how to use the standard library’s randomization functionality, how to extend the standard library in various ways, and a few key APIs that were introduced in Swift 5.1: