Swiftpack.co -  adil-hussain-84/ServiceLocator as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Swift package offering a simple in-memory bucket into which services can be registered and retrieved.
.package(url: "https://github.com/adil-hussain-84/ServiceLocator.git", from: "1.0.0")


Swift package offering a simple in-memory bucket into which services (i.e. objects) can be registered and retrieved.


  1. Add this Swift package to your Xcode project's list of Swift packages by means of this repo's URL.
  2. Select the target(s) that you would like to add this Swift package to.


The purpose of this Swift package is to offer a means by which an application can initialise and register all of its services into an in-memory bucket at start-up and then obtain these services later in the application's view controllers and tests without the view controllers and tests having to concern themselves with the details of how these services are initialised.

The main class offered by this Swift package is the ServiceLocator class. This class defines three methods as follows:

  • addService(service:) – Adds the given service to the service locator.
  • addModule(module:) – Adds each of the services defined in the given Module to the service locator.
  • getServiceOfType(type:) – Gets the service of the specified type from the service locator.

Registering services

Let's assume we're going to register services into the service locator grouped by modules rather than registering services into the service locator individually. Let's assume also, for the purposes of example, that our application consists of two modules – ModuleA and ModuleB – and ModuleA defines a service which a service in ModuleB depends on. Then, firstly, ModuleA will look something like:

import ServiceLocator

class ModuleA: Module {
    func getServices() -> [Any] {
        return [ServiceA()]

Secondly, ModuleB will look something like this:

import ServiceLocator

class ModuleB: Module {
    private let serviceLocator: ServiceLocator

    init(_ serviceLocator: ServiceLocator) {
        self.serviceLocator = serviceLocator

    func getServices() -> [Any] {
        let serviceA = try! serviceLocator.getServiceOfType(ServiceA.self)
        let serviceB = ServiceB(serviceA)
        return [serviceB]

Lastly, the ServiceLocator object will be defined as follows:

let serviceLocator = ServiceLocator()

try serviceLocator.addModule(ModuleA())
try serviceLocator.addModule(ModuleB(serviceLocator))

Retrieving services

The only two places where services should be sought out from a ServiceLocator object (once it has been defined and all services have been added to it) are the application's view controllers and test classes. All other calls to the ServiceLocator.getServiceOfType(type:) method are considered an abuse.

Assuming the presence of a ServiceLocator object named serviceLocator in a view controller or test class, a service is retrieved from the service locator as follows:

@Inject(via: serviceLocator) var someService: SomeService


Stars: 0
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API