Latest 3.7.1
Homepage https://github.com/ivlevAstef/DITranquillity
License MIT
Platforms ios 8.0, tvos 9.0, osx 10.10, requires ARC
Dependencies SwiftLazy
Authors

Carthage compatible
CocoaPods Version
License
Platform
Swift Version

The small library for dependency injection in applications written on pure Swift for iOS/OSX/tvOS. Despite its size, it solves a large enough range of tasks, including Storyboard support. Its main advantage – modularity of support, detailed errors description and lots of opportunities.

Features

Tranquillity

  • Pure Swift Type Support
  • Initializer injections ru
  • Property, Method injections ru
  • Named, Tags definitions and Many ru
  • Type forwarding ru
  • Lifetimes: single, perRun(weak/strong), perContainer(weak/strong), objectGraph, prototype, custom ru
  • iOS/macOS Storyboard and StoryboardReference ru
  • Circular dependencies ru
  • Three level hierarchy: types, part, framework ru
  • Short resolve syntax ru
  • keyPath injection (since swift4.0) ru
  • Very detail logs ru
  • Validation at the run app ru
  • Injection into Subviews and cells ru
  • Support Delayed injection ru
  • Injection with arguments at any depth
  • Container Hierarchy
  • Thread safe

Helpful links

Usage

// container - for register and resolve your types
let container = DIContainer()

container.register{ Cat(name: "Felix") }
  .as(Animal.self) // register Cat with name felix by protocol Animal
  .lifetime(.prototype) // set lifetime

container.register(PetOwner.init) // register PetOwner

// you can validate you registrations graph
if !container.validate() {
  fatalError("...")
}

.................................................

// get instance of a types from the container
let owner: PetOwner = container.resolve()
let animal: Animal = *container // short syntax

print(owner.pet.name) // "Felix"
print(animal.name) // "Felix"

.................................................

// where
protocol Animal {
  var name: String { get }
}

class Cat: Animal {
  let name: String
  init(name: String) {
    self.name = name
  }
}

class PetOwner {
  let pet: Animal
  init(pet: Animal) {
    self.pet = pet
  }
}
See More

“`Swift
let container = DIContainer()

container.register{ Cat(name: “Felix”) }
.as(Animal.self)

container.register{ Dog(name: “Rex”) }
.as(Animal.self)
.default()

container.register{ PetOwner(pets: many($0)) }
.injection(.home) // since swift4.0 and 3.2.0 lib

container.register(Home.init)
.postInit{ $0.address = “City, Street, Number” }

………………………………………….

let owner: PetOwner = *container

print(owner.pets.map{ $0.name }) // [“Felix”, “Rex”]
print(onwer.home.address) // “City, Street, Number”

………………………………………….

// where
protocol Animal {
var name: String { get }
}

class Cat: Animal {
let name: String
init(name: String) {
self.name = name
}
}

class Dog: Animal {
let name: String
init(name: String) {
self.name = name
}
}

class PetOwner {
let pets: [Animal]
init(pets: [Animal]) {
self.pets = pets
}

private(set) var home: Home!
}

class Home {
var address: String!
}
“`

Storyboard (iOS/OS X)

See code

Create your ViewController:
“`Swift
class ViewController: UIViewController/NSViewController {
private(set) var inject: Inject?

override func viewDidLoad() {
super.viewDidLoad()
print(“Inject: (inject)”)
}
}
“`
Create container:
“`Swift
let container = DIContainer()
container.register(ViewController.self)
.injection(.inject)
“`
Create Storyboard:
“`Swift
/// for iOS
func applicationDidFinishLaunching(_ application: UIApplication) {
let storyboard = DIStoryboard.create(name: “Main”, bundle: nil, container: container)

window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = storyboard.instantiateInitialViewController()
window!.makeKeyAndVisible()
}
“`

“`Swift
/// for OS X
func applicationDidFinishLaunching(_ aNotification: Notification) {
let storyboard = DIStoryboard.create(name: “Main”, bundle: nil, container: container)

let viewController = storyboard.instantiateInitialController() as! NSViewController
let window = NSApplication.shared.windows.first
window?.contentViewController = viewController
}
“`

Install

Via CocoaPods.

To install DITranquillity with CocoaPods, add the following lines to your Podfile: pod 'DITranquillity'

Via Carthage.

github "ivlevAstef/DITranquillity" Swift (iOS8+,macOS10.10+,tvOS9+)

Requirements

iOS 8.0+,macOS 10.10+,tvOS 9.0+; ARC

  • Swift 5.0: Xcode 10.2; version >= 3.6.3
  • Swift 4.2: Xcode 10; version >= 3.4.3
  • Swift 4.1: Xcode 9.3; version >= 3.2.3
  • Swift 4.0: Xcode 9.0; version >= 3.0.5
  • Swift 3.0-3.2: Xcode 8.0-9.0; 0.9.5 <= version < 3.7.0
  • Swift 2.3: Xcode 7.0; version < 0.9.5

Migration

  • v1.x.x -> v2.x.x ru
  • v2.x.x -> v3.x.x ru

Changelog

See CHANGELOG.md file.

Feedback

I’ve found a bug, or have a feature request

Please raise a GitHub issue.

I’ve found a defect in documentation, or thought up how to improve it

Please help library development and create pull requests

Question?

You can feel free to ask the question at e-mail: [email protected]

Latest podspec

{
    "name": "DITranquillity",
    "version": "3.7.1",
    "summary": "DITranquillity - Dependency injection for iOS/macOS/tvOS (Swift)",
    "description": "DITranquillity - The small library for dependency injection in applications written on pure Swift for iOS/OSX/tvOS. Despite its size, it solves a large enough range of tasks, including Storyboard support. Its main advantage - modularity of support, detailed logs and lots of opportunities.",
    "homepage": "https://github.com/ivlevAstef/DITranquillity",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "documentation_url": "https://htmlpreview.github.io/?https://github.com/ivlevAstef/DITranquillity/blob/master/Documentation/code/index.html",
    "authors": {
        "Alexander.Ivlev": "[email protected]"
    },
    "source": {
        "git": "https://github.com/ivlevAstef/DITranquillity.git",
        "tag": "v3.7.1"
    },
    "requires_arc": true,
    "dependencies": {
        "SwiftLazy": []
    },
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0",
        "osx": "10.10"
    },
    "ios": {
        "source_files": [
            "Sources/DITranquillity.h",
            "Sources/DITranquillity/**/*.swift",
            "Sources/Core/**/*.{h,m}",
            "Sources/DITranquillity/Hierarchy/*.swift",
            "Sources/DITranquillity/Extensions/*.swift",
            "Sources/InjectIntoSubviews/*.{h,m,swift}",
            "Sources/InjectIntoSubviews/iOS-tvOS/*.{h,m,swift}",
            "Sources/Storyboard/*.swift",
            "Sources/Storyboard/iOS-tvOS/*.{h,m}"
        ]
    },
    "tvos": {
        "source_files": [
            "Sources/DITranquillity.h",
            "Sources/DITranquillity/**/*.swift",
            "Sources/Core/**/*.{h,m}",
            "Sources/DITranquillity/Hierarchy/*.swift",
            "Sources/DITranquillity/Extensions/*.swift",
            "Sources/InjectIntoSubviews/*.{h,m,swift}",
            "Sources/InjectIntoSubviews/iOS-tvOS/*.{h,m,swift}",
            "Sources/Storyboard/*.swift",
            "Sources/Storyboard/iOS-tvOS/*.{h,m}"
        ]
    },
    "osx": {
        "source_files": [
            "Sources/DITranquillity.h",
            "Sources/DITranquillity/**/*.swift",
            "Sources/Core/**/*.{h,m}",
            "Sources/DITranquillity/Hierarchy/*.swift",
            "Sources/DITranquillity/Extensions/*.swift",
            "Sources/Storyboard/*.swift",
            "Sources/Storyboard/OSX/*.{h,m}"
        ]
    }
}

Pin It on Pinterest

Share This