https://github.com/bernndr/swift-macros
Collection of useful macros
https://github.com/bernndr/swift-macros
swift swift-macros swift-package swift-package-manager swift5-9 xcode xcode15
Last synced: 27 days ago
JSON representation
Collection of useful macros
- Host: GitHub
- URL: https://github.com/bernndr/swift-macros
- Owner: bernndr
- License: mit
- Created: 2023-11-21T09:27:03.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-10-06T10:38:15.000Z (5 months ago)
- Last Synced: 2025-11-18T09:24:44.358Z (4 months ago)
- Topics: swift, swift-macros, swift-package, swift-package-manager, swift5-9, xcode, xcode15
- Language: Swift
- Homepage:
- Size: 42 KB
- Stars: 34
- Watchers: 1
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift Macros
Contains a collection of useful macros for my personal projects.
## Usage
### Symbol
```swift
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
```swift
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.
```swift
@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
```swift
@Singleton
struct UserStore {
}
// Expands to
struct UserStore {
static let shared = UserStore()
private init() {
}
}
```