Skip to content

Latest commit

 

History

History
125 lines (100 loc) · 2.12 KB

README.md

File metadata and controls

125 lines (100 loc) · 2.12 KB

EnumPath

Swift Macro that generates enum's associated value getters, setters or boolean checks.

Swift Version

Problem

There is no way to create binding from enum's associated value.

enum Model {
    case none
    case label(String)
}

struct ContentView: View {
    @State var model: Model = .label("hello world")
 
    var body: some View {
        TextField("View Label", text: ...) // no way to create binding label's associated value
    }
}

Solution

Swift Macro EnumPath generates getters, setters and boolean checks for all enum cases.

@EnumPath
enum Model {
    case none
    case label(String)
}

Macro EnumPath expands into:

enum Model {
    case none
    case label(String)

    // generated by `EnumPath` macro
    var isNone: Bool {
        get {
            switch self {
            case .none: 
                true
            default: 
                false
            }
        }
        set {
            if newValue {
                self = .none
            }
        }
    }

    var label: String? {
        get {
            switch self {
            case let .label(label):
                label
            default:
                nil
            }
        }
        set {
            if let newValue {
                self = .label(newValue)
            }
        }
    }
}

After the macro expansion the usage can be:

@EnumPath
enum Model {
    case none
    case label(String)
}

struct ContentView: View {
    @State var model: Model = .label("hello world")
 
    var body: some View {
        if let label = Binding(model.label) {
            TextField("View Label", text: label)
        }
    }
}

Installation

Swift Package Manager

// In your `Package.swift`

dependencies: [
    .package(url: "https://github.com/artbobrov/EnumPath", branch: "main")
],
targets: [
    .target(
        name: ...,
        dependencies: [
            .product(name: "EnumPath", package: "EnumPath"),
            ...
        ]
    ),
    ...
]

Author