Using Xcode Extensions

Last week we took a look at automatically generating code through Sourcery, which generates code based on the code you’ve already written. But how about tools that generate all the code for you, where you don’t need any dependencies in your project? Let’s look at Xcode extensions.

Xcode extensions allow you to add functionality to the Xcode source editor to do various things for you. One of the most useful cases is being able to paste some JSON and have it automatically converted to Swift structs, along with CodingKeys, all for you. Or how about being able to create a mock class that conforms to your protocol and has everything implemented with default value with just a single button?

First let’s take a look at JSON converting. There are numerous tools to help you do this, but we’ll be taking a look at quicktype. https://github.com/quicktype/quicktype-xcode. This is an app you download in the Mac App Store, and when it’s downloaded, go ahead and click on it. It should install the extension to your Xcode automatically. And that’s it! Now just copy some JSON, open Xcode, and go to Editor -> Paste JSON as -> Paste JSON as Code. And tada! Your JSON will now be converted to Swift code for you!

Second, we’ll take a look at generating mocks from protocols you’ve written! Head over to here, https://github.com/seanhenry/SwiftMockGeneratorForXcode, head to the releases page and download the latest version. Once download is finished, click the app like before, reopen Xcode, and head back to the Editor menu. You should now see that you can create mock classes from protocols automatically!