/swift-case-accessors

Swift Macros for convenient access to enum case associated values

Primary LanguageSwiftMIT LicenseMIT

Case Accessors

Build & Test Swift Package Supports Swift 5.9+ Supports macOS, iOS, tvOS, watchOS, and Linux Licensed under the MIT License

Swift Package Index listingDocumentationCompatibility

This package offers macros that make destructuring enums with associated values more straightforward.

The @CaseAccessors macro adds computed properties to an enum type that allow easy retrieval of associated values.

@CaseAccessors enum TestEnum {
    case stringValue(String)
    case intValue(Int)
    case boolValue(Bool)
}

let enumValue = TestEnum.stringValue("Hello, Macros!")

if let stringValue = enumValue.stringValue {
    print(stringValue) // Prints "Hello, Macros!"
}

The second, @CaseConditionals adds boolean computed properties that make it easier to perform conditional checks on enums.

@CaseConditionals enum TestEnum {
    case one, two, three
}

let enumValue = TestEnum.one

if enumValue.isOne {
    // Do something
}

// The above is equivalent to
if case .one = enumValue {
    // Do something
}

Installation

Add the following to your Package.swift

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        // other dependencies
        .package(url: "https://github.com/rhysforyou/swift-case-accessors", "0.1.0"..<"0.2.0"),
    ],
    targets: [
        .target(
            name: "MyTarget",
            dependencies: ["CaseAccessors"]),
    ]
)