HIPInstanceLocator

@objc public class HIPInstanceLocator: NSObject

An implementation of the service locator pattern. Provides a place to register and get shared instances of classes for a specific context. Instances are registered using factory blocks, which are lazily instantiated on demand.

  • Initializes a locator instance; the assembly block is executed immediately after initialization.

    Declaration

    Swift

    public convenience init(assemblyBlock:HIPInstanceLocator -> Void)
  • Register an factory method for the specified type. The factory block is called the first time get() is called for this type. Subsequent calls to get() return cached instances.

    Example:

    locator.registerFactory(MyClass.self) {
       locator in
       return MyClass()
    }
    

    Returns

    true if the factory method was successfully registered, otherwise false

    Declaration

    Swift

    public func registerFactory<T>(key:T.Type, factory:HIPInstanceLocator -> T) -> Bool

    Return Value

    true if the factory method was successfully registered, otherwise false

  • Register an existing shared instance for the specified type. Instances registered this way are assumed to be retained elsewhere, so the locator will only hold a weak reference to it. Because the locator holds a weak reference, this method may only be used with class types.

    Note

    The shared instance must be a class instance.

    Example:

    let mySingleton = SingletonClass()
    locator.register(SingletonClass.self, mySingleton)  // stores a weak ref
    

    Returns

    true if the shared instance was successfully registered, otherwise false

    Declaration

    Swift

    public func register<T where T: AnyObject>(key:T.Type, sharedInstance: T) -> Bool

    Return Value

    true if the shared instance was successfully registered, otherwise false

  • Gets an instance of a previously registered type. If an instance was not already created, it will be initialized. If this type was not registered, this returns nil and throws a debug assert.

    You’ll typically use this inside a HIPInstanceLocator.injectInstancesOf(_:injector:) block.

    Note

    You may want to use HIPInstanceLocator.implicitGet() instead for most cases.

    Parameter

    Parameter _: Type of instance to get.

    Example

    Declaration

    Swift

    public func getInstanceOf<T>(_:T.Type) -> T!
  • Implicitly gets an instance of a previously registered type. If an instance was not already created, it will be initialized. If this type was not registered, this returns nil and throws a debug assert. Use this method when the return type can be inferred, such as initializer or method parameters.

    This method is made possible by Swift’s type inference. The left side of the assignment expression typically has an explicit type, so you can just say give me whatever I’m asking for.

    You’ll typically use this inside a HIPInstanceLocator.injectInstancesOf(_:injector:) block.

    Example:

    let standardBackground = UIColor.redColor()
    locator.injectInstancesOf(MyViewController.self) {
       locator, viewController in in
       viewController.myColor = locator.implicitGet()  // magic!
    }
    

    Declaration

    Swift

    public func implicitGet<T>() -> T!
  • Registers an injector method for the specified type. The injector method is called once for each instance created by the instance locator. The injector is also used for objects initialized in storyboards.

    Example:

    locator.injectInstancesOf(MyViewController.self) {
       locator, viewController in
       viewController.someDependency = locator.implicitGet()
    }
    

    Returns

    true if the instance was injected, otherwise false

    Declaration

    Swift

    public func injectInstancesOf<T>(key: T.Type, injector:((HIPInstanceLocator, T) -> Void)) -> Bool

    Return Value

    true if the instance was injected, otherwise false

  • Injects instance using the block specified for T in HIPInstanceLocator.injectInstancesOf(_:injector:)

    Declaration

    Swift

    public func applyInjector<T>(instance: T) -> Bool
  • Applies one or more assemblies to this instance locator. Each assembly is applied in order. If the same dependency is registered or injected more than once, then this will throw a debug assert in the same way that would happen if those dependencies were registered or injected outside of an assembly.

    Declaration

    Swift

    public func assemble(assemblies: HIPInstanceLocatorAssembly...)
  • Get an instance for a class without any fancy type inference.

    Declaration

    Swift

    @objc public func objc_getInstanceOfClass(aClass: AnyClass) -> AnyObject!
  • Applies previously registered injector to an instance without any type inference.

    Declaration

    Swift

    @objc public func objc_applyInjector(aClass: AnyClass, toInstance instance: AnyObject) -> Bool