GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

< Back to Latest Articles

Introduction – please watch!

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.

Watch the video here, or read the article below

Quick links

How to read the code

Large parts of Swift is written in C++, but almost as much is written in Swift itself – almost all the Swift standard library, for example, is written in Swift code, and therefore makes for excellent learning material if you know where to look.

In this series I'll be picking out various aspects of Swift and the standard library from Apple's own code. As a result, it's important you have easy access to that code, and here there are two options:

  • You can clone the Swift repository from https://github.com/apple/swift, giving you access to the code on your desktop.
  • You can also browse all the source code online through your web browser at that same URL.

Both work, but the former is significantly faster and easier to use – you can move through directories in Finder, open files in Xcode, search through file contents using your preferred tools, and more.

In this series I'll be using the local clone option. So, you'll see me browse through the directories locally on my Mac and open code right inside Xcode. If you want the easiest approach to follow along, I highly recommend you do the same.

If you're using the command-line, running git clone http://github.com/apple/swift should be enough to get you a full copy of the files. Inside there the most important directory for us is stdlib > public > core, where all the base standard library files are.

A brief explanation of GYB

The standard library is made up of a whole bunch of Swift files, but there is one important exception: files that end in "gyb".

GYB stands for "generate your boilerplate", and it's used by the Swift team to generate Swift code for times when there is lots of duplication. For example, FloatingPointTypes.swift.gyb generates the types Float, Double, and more – types that are the same apart from the size of their storage.

So, rather than the Swift team having lots of code duplicated in places, they instead run a little Python code create all those types from one template. These files contain Python code such as conditions and loops to customize each type as needed, but the result is it generates a bunch of Swift files that can then be built for real to make the standard library.

A reminder to update

Before I'm done with this intro, I want to mention one important thing: Swift is an actively developed project by Apple, so the code we're examining might change in the future. That's okay, though: we're not trying to actually memorize Apple's code, we're just looking for ways they've used Swift and how we can use similar techniques ourselves.

That being said, if you find me referencing some code that doesn't exist in your version of the code, your first port of call should be to update your local clone of the repository just in case your version is out of date.

If you liked this, you'd love Hacking with Swift+…

Here's just a sample of the other tutorials, with each one coming as an article to read and as a 4K Ultra HD video.

Find out more and subscribe here


Shadows and glows

19:50

SWIFTUI SPECIAL EFFECTS

FREE: Shadows and glows

SwiftUI gives us a modifier to make simple shadows, but if you want something more advanced such as inner shadows or glows, you need to do extra work. In this article I’ll show you how to get both those effects and more in a customizable, flexible way.

Controlling views using the accelerometer

39:03

SWIFTUI SPECIAL EFFECTS

FREE: Controlling views using the accelerometer

Reading device motion and orientation is a fast and slightly magical way to incorporate the real world into your apps, and can do a huge amount to add a little spark of delight to your UI. In this article I’m going to show you how easy it is to control SwiftUI layouts using the accelerometer, and give you a few ideas for special effects.

Creating a WaveView to draw smooth waveforms

32:08

CUSTOM SWIFTUI COMPONENTS

FREE: Creating a WaveView to draw smooth waveforms

In this article I’m going to walk you through building a WaveView with SwiftUI, allowing us to create beautiful waveform-like effects to bring your user interface to life.

Ultimate Portfolio App: Introduction

11:03

ULTIMATE PORTFOLIO APP

FREE: Ultimate Portfolio App: Introduction

While I’m sure you’re keen to get started programming immediately, please give me a few minutes to outline the goals of this course and explain why it’s different from other courses I’ve written.

Understanding generics – part 1

20:01

INTERMEDIATE SWIFT

FREE: Understanding generics – part 1

Generics are one of the most powerful features of Swift, allowing us to write code once and reuse it in many ways. In this article we’ll explore how they work, why adding constraints actually helps us write more code, and how generics help solve one of the biggest problems in Swift.

The pitfalls of string bridging

23:09

LEARN SOMETHING NEW

FREE: The pitfalls of string bridging

Swift's strings are designed to work flawlessly with languages around the world, but sometimes – just sometimes – you need to be careful using them. Let's explore why…

Transforming data with map()

42:32

FUNCTIONAL PROGRAMMING

FREE: Transforming data with map()

In this article we’re going to look at the map() function, which transforms one thing into another thing. Along the way we’ll also be exploring some core concepts of functional programming, so if you read no other articles in this course at least read this one!

Interview questions: Introduction

3:54

INTERVIEW QUESTIONS

FREE: Interview questions: Introduction

Getting ready for a job interview is tough work, so I’ve prepared a whole bunch of common questions and answers to help give you a jump start. But before you get into them, let me explain the plan in more detail…

Using memoization to speed up slow functions

36:18

HIGH-PERFORMANCE APPS

FREE: Using memoization to speed up slow functions

In this article you’ll learn how memoization can dramatically boost the performance of slow functions, and how easy Swift makes it thanks to its generics and closures.

Brush & Bark

2:16:15

LIVE STREAMS

FREE: Brush & Bark

In this stream we're going to build a website in Swift, using a free, open-source framework I produced called Ignite. It's designed to be familiar for SwiftUI developers, so hopefully you can see the appeal!

Understanding assertions

27:33

INTERMEDIATE SWIFT

FREE: Understanding assertions

Assertions allow us to have Swift silently check the state of our program at runtime, but if you want to get them right you need to understand some intricacies. In this article I’ll walk you through the five ways we can make assertions in Swift, and provide clear advice on which to use and when.

Trees

31:55

DATA STRUCTURES

FREE: Trees

Trees are an extraordinarily simple, extraordinarily useful data type, and in this article we’ll make a complete tree data type using Swift in just a few minutes. But rather than just stop there, we’re going to do something quite beautiful that I hope will blow your mind while teaching you something useful.

Creating a custom property wrapper using DynamicProperty

14:20

INTERMEDIATE SWIFTUI

FREE: Creating a custom property wrapper using DynamicProperty

It’s not hard to make a basic property wrapper, but if you want one that automatically updates the body property like @State you need to do some extra work. In this article I’ll show you exactly how it’s done, as we build a property wrapper capable of reading and writing documents from our app’s container.

How to use phantom types in Swift

24:11

ADVANCED SWIFT

FREE: How to use phantom types in Swift

Phantom types are a powerful way to give the Swift compiler extra information about our code so that it can stop us from making mistakes. In this article I’m going to explain how they work and why you’d want them, as well as providing lots of hands-on examples you can try.

User-friendly network access

14:26

NETWORKING

FREE: User-friendly network access

Anyone can write Swift code to fetch network data, but much harder is knowing how to write code to do it respectfully. In this article we’ll look at building a considerate network stack, taking into account the user’s connection, preferences, and more.

Functional programming in Swift: Introduction

6:52

FUNCTIONAL PROGRAMMING

FREE: Functional programming in Swift: Introduction

Before you dive in to the first article in this course, I want to give you a brief overview of our goals, how the content is structured, as well as a rough idea of what you can expect to find.

Making the most of optionals

23:07

ADVANCED SWIFT

FREE: Making the most of optionals

Swift’s optionals are implemented as simple enums, with just a little compiler magic sprinkled around as syntactic sugar. However, they do much more than people realize, and in this article I’m going to demonstrate some of their power features that can really help you write better code – and blow your mind along the way.

Views and Modifiers

5:43

SOLUTIONS

Views and Modifiers

This challenge asks you to go back and adjust both project 1 and project 2 based on what you learned, then try your hand at creating a custom view modifier. Let’s tackle it now…

Implementing App Clips in a real app

1:01:00

EVENTS

Implementing App Clips in a real app

Now that you understand how App Clips work, in this part we’ll apply them to our Barking Lot app so you can see them in action with real code.

From grids to map annotations

1:04:20

EVENTS

From grids to map annotations

We’re going to upgrade our map with annotations, but first I want to give you the chance to implement grids in our Journeys app so you can see them in action with your own code.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.