Inside Swift
At the end of each article, press the "Mark this article as read" button to have the site remember you've read it.
The Inside Swift series is designed to explore Swift's own source code, so you can better understand how it works and also pick up techniques you can apply to your own code.
Sometimes less is more, and Swift's StaticString
is a great example of how adding restrictions actually adds usefulness.
Increasing clarity with function attributes
Swift does a great job of throwing up warnings and compile errors when our code isn't quite right, but by giving it a little extra information we can help control those warnings more precisely.
How to make abstract classes in Swift
Swift doesn't support abstract classes, but sometimes we really need them. Helpfully, we can find a solution of sorts right inside Swift's own source code…
Swift has a dedicated zip()
function that creates zipped sequences. This is not only useful, but also a great safety improvement for some code, and the standard library code for it is delightfully simple.
Although Swift is a very strict language, it also sprinkles a great deal of syntactic sugar around to make our lives easier. Let's explore a part of that here: types that are expressible as array literals…
Sometimes the safest thing to do is crash your code. That might sound odd, but it's true, and in this article I'll show you exactly how Swift does it…
Typed throws were introduced in Swift 6 to let us handle errors more neatly, and a great way to explore them is with Swift's own source code.
I want to look at a bafflingly brilliant piece of the Swift standard library: the allSatisfy()
method of sequences, which I think is quite literally perfect. Let's get into it!
Literal strings and string interpolation
Have you ever wondered how string interpolation works in Swift? It's another important feature of the standard library, and even gets extensive use in SwiftUI! Let's dig in…
What lets us compare two arrays to see if they are the same? How can Swift encode and decode any optional value as long as the type it wraps is also Codable
? The answer is conditional conformances, and they are one of my favorite features of Swift!
Collections like arrays and sets are how we store lots of objects in a single place, but what if we wanted to store only one item? More importantly, why would we want to store only one item?
Option sets are a feature so small and clever that you barely know they exist, but Apple uses them extensively and you should too.