Contents

bernndr/swift-macros

Collection of useful macros

Usage

Symbol

let symbol = #symbol("swift") // Macro expands to "swift"

In case the provided value is not a valid SF Symbol, Xcode will show a compile error.

URL

let url = #URL("https://www.swift.org") // Macro expands to URL(string: "https://www.swift.org")!

In case the provided value is not a valid URL, Xcode will show a compile error.

AssociatedValues

Add variables to retrieve the associated values.

@AssociatedValues
enum Barcode {
  case upc(Int, Int, Int, Int)
  case qrCode(String)
}

// Expands to
enum Barcode {
  ...

  var upcValue: (Int, Int, Int, Int)? {
    if case let .upc(v0, v1, v2, v3) = self {
        return (v0, v1, v2, v3)
    }
    return nil
  }

  var qrCodeValue: (String)? {
    if case let .qrCode(v0) = self {
        return v0
    }
    return nil
  }
}

Singleton

Generate singleton code for struct and class

@Singleton
struct UserStore {
}

// Expands to
struct UserStore {
  static let shared = UserStore()

  private init() {
  }
}

Package Metadata

Repository: bernndr/swift-macros

Stars: 35

Forks: 9

Open issues: 1

Default branch: main

Primary language: swift

License: MIT

Topics: swift, swift-macros, swift-package, swift-package-manager, swift5-9, xcode, xcode15

README: README.md