Dependency injector Swift support ObjectMapper

What's New



fix build error


Swift Version Build Status CocoaPods Compatible Carthage Compatible Swift Package Manager Compatible Platform Twitter

Dependency injector Swift compatible with ObjectMapper



To install DependencyInjector+ObjectMapper with CocoaPods, add the following lines to your Podfile.

source ''
platform :ios, '9.0'

pod 'DependencyInjectorObjectMapper'


Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate DependencyInjector+ObjectMapper into your Xcode project using Carthage, specify it in your Cartfile:

github 'Digipolitan/dependency-injector-object-mapper' ~> 1.0

Run carthage update to build the framework and drag the built DependencyInjectorObjectMapper.framework into your Xcode project.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding DependencyInjector+ObjectMapper as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "", from: "1.0.0")

How to use ?

First you must create your models conforms to BaseMappable protocol

public protocol User: BaseMappable {
    var id: String { get set }
    var created: Date? { get set }
    var address: Address { get set }

public protocol Address: BaseMappable {
    var street: String { get set }
    var zipCode: String { get set }
    var country: String { get set }

After that, create the real implementation conforms to Mappable protocol foreach models

class UserModel: User, Mappable {
    public var id: String
    public var created: Date?
    public var address: Address

    public init(id: String, address: Address) { = id
        self.address = address

    public convenience required init?(map: Map) {
            let id: String = try? map.value("id"),
            let address: Address = try? map.injectedValue("address", type: Address.self) else {
                return nil
        self.init(id: id, address: address)

    public func mapping(map: Map) { >>> map["id"]
        self.created <- (map["createdAt"], DateTransform())
        self.address >>> map.inject("address", type: Address.self)

class AddressModel: Address, Mappable {
    public var street: String
    public var zipCode: String
    public var country: String

    public init(street: String, zipCode: String, country: String) {
        self.street = street
        self.zipCode = zipCode = country

    public convenience required init?(map: Map) {
        guard let street: String = try? map.value("street"), let zipCode: String = try? map.value("zipCode"), let country: String = try? map.value("country") else {
            return nil
        self.init(street: street, zipCode: zipCode, country: country)

    public func mapping(map: Map) {
        self.street >>> map["street"]
        self.zipCode >>> map["zipCode"] >>> map["country"]
  • For nonnull property set the value inside the initializer

  • To inject a custom implementation use map.injectedValue during the init process, or map.inject in the mapping function

After that, you must register implementation inside a module and push the module inside an injector

class DefaultModule: Module {

    override init() {
Injector.default.register(module: DefaultModule(), with: "default")

Finally, inject the User model as follow

let user = try? Injector.default.inject(User.self, arguments: [
    "id": "1",
    "address": [
        "street": "abc",
        "zipCode": "75116",
        "country": "France"


See for more details!

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to


DependencyInjector+ObjectMapper is licensed under the BSD 3-Clause license.


  • Swift Tools 4.0.0
View More Packages from this Author


Last updated: Wed Nov 09 2022 06:20:34 GMT-0500 (GMT-05:00)