Article
Introduction to SwiftPoet
July 2, 2018
SwiftPoet is a powerful framework built by Gilt Tech to let you generate your own Swift code… using Swift!
Why do this? Sometimes the job of a developer is to crank out some boilerplate code. It’s not all brilliant problem solving. Developers are great at recognizing patterns, and if it’s possible to automate something, we’ll do it.
I’m using SwiftPoet in a command line tool to translate Avro schema definitions into Swift structs that are customized to my liking. (To make your own — check out my article on how to setup a Swift Command Line Tool with a dependency .)
For reference, there is an API doc , but it doesn’t show you how to get started. I aim to rectify that in this article.
The goal is to generate a String
Ultimately we want to generate some Swift code. SwiftPoet provides that to us in String
form.
To do that, create a PoetFile
object and retrieve the generated Swift through the fileContents
property: