← Back to list

Part 1: What Are Swift Macros and Why Do We Need Them?

If you have ever found yourself copying and pasting the exact same lines of Swift code over and over again, you are not alone. To solve…

Saurav Sagar · 2026-05-25 10:31 · 7 claps · 2.7 min read paywalled
#macro #swift-macros #swift #swiftui #ios-development
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

Part 1: What Are Swift Macros and Why Do We Need Them?

If you have ever found yourself copying and pasting the exact same lines of Swift code over and over again, you are not alone. To solve this problem, Apple introduced Macros.

In simple terms, you can think of macros as mini-programs that generate code for you at compile time. Macros transform your source code when you compile it, which lets you avoid writing repetitive, boilerplate code by hand. During compilation, Swift looks for these macros and “expands” them — meaning it replaces the macro with the actual, generated Swift code before building your app.

Are Macros Safe to Use?

You might be worried that letting a program write code for you could break your app. Fortunately, Swift macros are designed to be extremely safe:

  • They are additive: Expanding a macro is always an additive operation. This means macros can only add new code; they are never allowed to delete or modify the code you have already written.
  • They are strictly checked: Both the inputs you give to a macro and the output code it generates are checked by the compiler to ensure they are syntactically valid Swift code with the correct types. If a macro makes a mistake, you just get a standard compile-time error.

Predefined Macros in Swift

You might have already used macros without even realizing it! Swift comes with several predefined macros built right into the standard library. Let’s look at two common ones:

  1. function: This macro acts as a placeholder. When you compile your code, Swift calls this macro’s implementation, which replaces #function with the actual name of the function it is currently inside. It is incredibly useful for debugging or logging.

  2. warning: This macro performs an action at compile time. It allows you to produce a custom warning message that will show up in Xcode to remind you (or your teammates) to fix or update something.

The Two Types of Macros

Swift divides all macros into two main categories:

1. Freestanding Macros Freestanding macros appear on their own in your code, without needing to be attached to anything else.

  • How to spot them: You call a freestanding macro by writing a number sign (#) before its name, followed by any arguments in parentheses.
  • Example: #function and #warning are freestanding macros. They can either produce a specific value or perform an action during compilation.

2. Attached Macros Attached macros are designed to modify a specific declaration (like a struct, class, or enum) that they are attached to. They add new code directly to that declaration, such as automatically creating a new method or making the type conform to a protocol.

  • How to spot them: You call an attached macro by writing an at sign (@) before its name.
  • Example: If you use @OptionSet on a structure, the macro will read your code and automatically generate all the repetitive static variables and initializers required to make it a functional option set.

Let me know when you have read through this, and tell me if you are ready to move on to Part 2, where we will explore exactly how Swift expands these macros under the hood and look closer at the differences between macro types!

Source: Information drawn from the official Swift Documentation: “Macros”, The Swift Programming Language.

PART 2: https://medium.com/@sagarsaurav/00d52c550012


메타데이터
post_id
e7c7b0a7201f
slug
part-1-what-are-swift-macros-and-why-do-we-need-them-e7c7b0a7201f
url
https://medium.com/@sagarsaurav/part-1-what-are-swift-macros-and-why-do-we-need-them-e7c7b0a7201f
canonical_url
https://medium.com/@sagarsaurav/part-1-what-are-swift-macros-and-why-do-we-need-them-e7c7b0a7201f
author_url
https://medium.com/@sagarsaurav
status
ok
fetched_at
2026-06-25 12:15:08