Part 2: Enhancing Constant Value Management in Swift Enums using Macros

VINODH KUMAR
3 min readMar 1, 2024

--

Photo by AltumCode on Unsplash

Introduction:

In Part 1 of this series, we explored various techniques for storing constant values in Swift using structs and enums. Building upon that foundation, we delve into an advanced approach, leveraging computed properties within enums to dynamically generate raw values using macros. However, we also address the challenges posed by the previous method involving the Functionable protocol, which required calling enum members like functions, potentially causing confusion.

Computed Properties Inside Enums:

Enums in Swift are powerful constructs capable of hosting computed properties alongside cases and raw values. By incorporating computed properties, we can significantly enhance the flexibility and extensibility of enums, enabling dynamic behavior within our codebase.

Addressing Confusion:

While the Functionable protocol presented a solution in Part 1, its implementation introduced potential confusion, as enum members needed to be called like functions. To address this, we shift our focus to using computed properties within enums for a more intuitive approach.

enum FontSize: Double {
case headline = 34
case body = 18
}

extension FontSize {
static var _headline: Double { headline.rawValue }
static var _body: Double { body.rawValue }
}

Here, the enum FontSize returns constant double values for font sizes, while the extension provides computed properties to access the raw values of each case. To avoid naming conflicts, we prefix the case names with an underscore.

The Problem and Solution:

However, manually adding computed properties for each case introduces maintenance overhead. The solution lies in harnessing the power of macros, particularly Apple’s macros, to automate this process.

If you are new to macros, i highly encourage you to read this series which explains about the Macros in Swift.

Unveiling the Power of Macros in Swift

Freestanding Macros

Empowering Swift Development with Freestanding Expression Macros: A Case Study of the URL Macro

Exploring Freestanding Declaration Macros in Swift.

Attached Macros

Unlocking Swift’s Potential: Empowering Declarations with Peer Macros

Unleashing Swift’s Potential: Enhancing Declarations with Member Macros.

Exploring Member Attribute Macros in Swift.

Exploring Accessor Macros in Swift.

Exploring Extension Macros in Swift.

Introducing EnumValueGeneratorMacro:

To streamline this process, we introduce the EnumValueGeneratorMacro library. This macro automatically generates the necessary extensions with computed properties for all cases of the enum, eliminating the need for manual intervention.

Installation:

You can install EnumValueGeneratorMacro via Swift Package Manager. Add the following dependency to your Package.swift file:

dependencies: [
.package(url: "https://github.com/vinodh06/EnumValueGenerator.git", from: "1.0.0")
]

Usage:

Import the EnumValueGeneratorMacro module into your Swift file.

import EnumValueGeneratorMacro

By simply annotating the enum declaration with @enumValueGenerator, the macro expands the code, generating the required extensions.

@enumValueGenerator
enum FontSize: Double {
case headline = 34
case body = 18
}

EnumValueGenerator will create an extension with computed properties similar to the case names, prefixed with an underscore `_`.

extension FontSize {
static var _headline: Double { headline.rawValue }
static var _body: Double { body.rawValue }
}

Upon invocation, the macro expands the enum declaration to include extensions with computed properties for each case, as demonstrated above and we can access the computed property directly.

Here is the GitHub link for EnumValueGenerator macro library

https://github.com/vinodh06/EnumValueGeneratorMacro

Conclusion:

Part 2 of this series has unveiled a more intuitive and efficient approach to managing constant values in Swift enums. By leveraging computed properties and macros, developers can streamline code maintenance and enhance readability, ensuring a more robust and scalable codebase.

We encourage further exploration and welcome your feedback on this advanced method of constant value management in Swift enums. Stay tuned for future advancements and enhancements in Swift development.

--

--

VINODH KUMAR

📱 Senior iOS Developer | Swift Enthusiast | Tech Blogger 🖥️ Connect with me on linkedin.com/in/vinodhkumar-govindaraj-838a85100