https://github.com/vincent-pradeilles/locatable
A micro-framework that leverages Swift Property Wrappers to implement the Service Locator pattern
https://github.com/vincent-pradeilles/locatable
Last synced: 1 day ago
JSON representation
A micro-framework that leverages Swift Property Wrappers to implement the Service Locator pattern
- Host: GitHub
- URL: https://github.com/vincent-pradeilles/locatable
- Owner: vincent-pradeilles
- License: mit
- Created: 2019-06-07T00:05:05.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-04-02T14:50:05.000Z (about 4 years ago)
- Last Synced: 2025-04-11T19:55:08.078Z (3 days ago)
- Language: Swift
- Homepage:
- Size: 30.3 KB
- Stars: 118
- Watchers: 2
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swift - Locatable - A micro-framework that leverages Property Wrappers to implement the Service Locator pattern. (Libs / Dependency Injection)
- awesome-swift - Locatable - A micro-framework that leverages Property Wrappers to implement the Service Locator pattern. (Libs / Dependency Injection)
- awesome-ios-star - Locatable - A micro-framework that leverages Property Wrappers to implement the Service Locator pattern. (Dependency Injection / Getting Started)
- fucking-awesome-swift - Locatable - A micro-framework that leverages Property Wrappers to implement the Service Locator pattern. (Libs / Dependency Injection)
- awesome-ios - Locatable - A micro-framework that leverages Property Wrappers to implement the Service Locator pattern. (Dependency Injection / Getting Started)
- fucking-awesome-ios - Locatable - A micro-framework that leverages Property Wrappers to implement the Service Locator pattern. (Dependency Injection / Getting Started)
- awesome-swift - Locatable - A micro-framework that leverages Swift Property Wrappers to implement the Service Locator pattern ` 📝 2 years ago ` (Dependency Injection [🔝](#readme))
README
# Locatable


[](https://github.com/Carthage/Carthage)## Context
Locatable is a Swift micro framework that leverages Property Wrappers to implement the Service Locator pattern, through a custom attribute `@Locatable`.
Here's an example of how it can be used:
```swift
protocol Servicing {
func action()
}class Service: Servicing {
func action() {
print("I'm performing a service 😊")
}
}Locator.register(Servicing.self, { return Service() })
class MyController {
@Locatable(.sharedInstance) var service: Servicing
func work() {
self.service.action()
}
}let controller = MyController()
controller.work() // I'm performing a service 😊
```For convenience, some shorthand syntax are also available:
```swift
// leverages @autoclosure
Locator.register(Servicing.self, Service())// leverages default argument values
Locator.register { return Service() as Servicing }
```Service locating supports two distinct semantics:
```swift
// Will return an instance that is shared across the app
Locatable(.sharedInstance) var service: Servicing// Will return a new instance every time
Locatable(.newInstance) var service: Servicing
```## Requirements
Xcode 11+ & Swift 5.1
## Installation
### CocoaPods
Add the following to your `Podfile`:
`pod "Locatable"`
### Carthage
Add the following to your `Cartfile`:
`github "vincent-pradeilles/locatable"`
## Author
* Twitter: [@v_pradeilles](https://twitter.com/v_pradeilles)