A practical collection of Swift Macros that help code correctly and swiftly.
.package(url: "https://github.com/ShenghaiWang/SwiftMacros.git", from: "2.0.0")
Macros API Doc
Macro | Description |
---|---|
@Access | An easy API to access UserDefaults, Keychain, NSCache and NSMapTable. |
struct TestAccess { |
|
@AddAssociatedValueVariable | Add variables to retrieve the associated values |
@AddAssociatedValueVariable |
|
@AddInit | Generate initialiser for the class/struct/actor. The variables with optional types will have nil as default values. Using withMock: true if want to generate mock data. For custmoised data type, it will use Type.mock . In case there is no this value, need to define this yourself or use @Mock or @AddInit(withMock: true) to generate this variable. |
@AddPublisher | Generate a Combine publisher to a Combine subject in order to avoid overexposing subject variable |
@AddPublisher |
|
@AddInit |
|
#buildDate | Build a Date from components This solution addes in a resultBulder DateBuilder , which can be used directly if prefer builder pattern.Note: this is for a simpler API. Please use it with caution in places that require efficiency. |
let date = #buildDate(DateString("03/05/2003", dateFormat: "MM/dd/yyyy"), |
|
#buildURL | Build a url from components. This solution addes in a resultBulder URLBuilder , which can be used directly if prefer builder pattern. |
let url = #buildURL("http://google.com", |
|
#buildURLRequest | Build a URLRequest from components. This solution addes in a resultBulder URLRequestBuilder , which can be used directly if prefer builder pattern. |
let urlrequest = #buildURLRequest(url!, RequestTimeOutInterval(100)) |
|
@ConformToEquatable | Add Equtable conformance to a class type Use it with caution per https://github.com/apple/swift-evolution/blob/main/proposals/0185-synthesize-equatable-hashable.md#synthesis-for-class-types-and-tuples |
@AddInit |
|
@ConformToHashable | Add Hashable conformance to a class type Use it with caution per https://github.com/apple/swift-evolution/blob/main/proposals/0185-synthesize-equatable-hashable.md#synthesis-for-class-types-and-tuples |
@AddInit |
|
#encode | Encode an Encodable to data using JSONEncoder |
#encode(value) |
|
#decode | Decode a Decodable to a typed value using JSONDecoder |
#decode(TestStruct.self, from: data) |
|
#formatDate | Format date to a string |
#formatDate(Date(), dateStyle: .full) |
|
#formatDateComponents | Format date differences/timeinterval/date components to a string |
#formatDateComponents(from: Date(), to: Date(), allowedUnits: [.day, .hour, .minute, .second]) |
|
#formatDateInterval | Format two dates into interval string |
#formatDateInterval(from: Date(), to: Date(), dateStyle: .short) |
|
@Mock | Generate a static variable mock using the attached initializer. For custmoised data type, it will use Type.mock . In case there is no this value, need to define this yourself or use @Mock or @AddInit(withMock: true) to generate this variable. |
class AStruct { |
|
#postNotification | An easy way to post notifications |
#postNotification(.NSCalendarDayChanged) |
|
@Singleton | Generate Swift singleton code for struct and class types |
@Singleton |
Please feel free to add the macros you want here. All PRs(with or without implementations) are welcome.
Macro | Description |
---|---|
@ | Your new macro ideas |