Home

Awesome

Locatable

platforms pod Carthage compatible

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:

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:

// leverages @autoclosure
Locator.register(Servicing.self, Service())

// leverages default argument values
Locator.register { return Service() as Servicing }

Service locating supports two distinct semantics:

// 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