Creating our first Swift macro
As I mentioned earlier (in the What is a Swift macro? section), the Swift Macros feature is part of the SwiftSyntax
library. Macros don’t run as part of our app but as a plugin in the IDE.
Macros can be created by adding a new Swift package with a macro template.
It is obvious why Apple selected the Swift package feature to create macros – a Swift package is a great way to encapsulate code, including tests and documentation.
Let’s add our first Swift macro by creating a new Swift package.
Adding a new Swift macro
To create a new Swift macro, we should open Xcode and follow these steps:
- Select File | New | Package….
- Then, select Swift Macro followed by tapping on Next (see Figure 10.5):
Figure 10.5: Selecting Swift Macro in the choose template window
- In the opening screen, we will give a name for our struct and press the Create button. As part of our learning...