This repository contains a simple implementation of a swift macro that allows you to create singletons in a more concise way.
- Add a package dependecy to your project
- In order to apply a macro use
@Singleton
annotation before the class definition. Like this:
import Singleton
@Singleton
class MyClass {
// class contents...
}
- The macro will expand the class definition to a singleton implementation like this:
import Singleton
class MyClass {
static let shared = MyClass()
private init() {
}
}
- Thus making a
MyClass.shared
instance of the class available out of the box.