CaseAccessors

0.2.0

Swift Macros for convenient access to enum case associated values
rhysforyou/swift-case-accessors

What's New

2023-07-07T06:50:53Z

Fixed

  • Enum cases with multiple associated values no longer produce a build warning

Case Accessors

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.string("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"]),
    ]
)

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Sun Oct 20 2024 12:50:09 GMT-0900 (Hawaii-Aleutian Daylight Time)