There's one technique I've been patiently waiting to show you since this series started, and now is the right time: extensions. Cunningly, this is not at all the same thing I already showed you in project 16, which was when we created an extension to Safari. This time we're going to create extensions for Swift – literally extending the language so it can do more things.
This isn't complicated – honest! We're not trying to make Swift do things it wasn't designed to do; in fact, I'd wager that language extensions are used in nearly all major projects that are shipping today. Why? Because they let you attach functionality to data types you didn't create. You've seen time and time again how we can add any methods we want to our own classes. Well, extensions let you do that to other classes and structs, including Apple's own.
There isn't enough in extensions to give you an intellectual challenge, so you're going to learn about extensions while using a Swift playground. We haven't used these yet because they haven't been appropriate, but now is a good chance. I'm also going to take this opportunity to outline in more detail the differences between functions and methods.
In Xcode, go to the File menu and choose New > Playground. Name it Project24, make sure iOS is selected as the platform, then choose Next and save it somewhere you can find later. Swift playgrounds are split into two columns: the left half contains your code, the right contains your output.
Want to build macOS apps?
Hacking with macOS delivers 18 awesome projects that teach you macOS development in no time – take control of your desktop today!