All Projects → Liftric → Dikit

Liftric / Dikit

Licence: mit
Dependency Injection Framework for Swift, inspired by KOIN.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Dikit

Swipycell
Easy to use UITableViewCell implementing swiping to trigger actions.
Stars: ✭ 230 (+198.7%)
Mutual labels:  cocoapods, carthage, swift-package-manager, swiftpm
Preferences
⚙ Add a preferences window to your macOS app in minutes
Stars: ✭ 898 (+1066.23%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Statusalert
Display Apple system-like self-hiding status alerts. It is well suited for notifying user without interrupting user flow in iOS-like way.
Stars: ✭ 809 (+950.65%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Swiftlyext
SwiftlyExt is a collection of useful extensions for Swift 3 standard classes and types 🚀
Stars: ✭ 31 (-59.74%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Guitar
A Cross-Platform String and Regular Expression Library written in Swift.
Stars: ✭ 641 (+732.47%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Zephyr
Effortlessly synchronize UserDefaults over iCloud.
Stars: ✭ 722 (+837.66%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Cameramanager
Simple Swift class to provide all the configurations you need to create custom camera view in your app
Stars: ✭ 1,130 (+1367.53%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Keyboardshortcuts
Add user-customizable global keyboard shortcuts to your macOS app in minutes
Stars: ✭ 500 (+549.35%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Scenekit Bezier Animations
Create animations over Bezier curves of any number of points
Stars: ✭ 35 (-54.55%)
Mutual labels:  cocoapods, swift-package-manager, swiftpm
Sica
🦌 Simple Interface Core Animation. Run type-safe animation sequencially or parallelly
Stars: ✭ 980 (+1172.73%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Fontblaster
Programmatically load custom fonts into your iOS and tvOS app.
Stars: ✭ 1,000 (+1198.7%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Sablurimageview
You can use blur effect and it's animation easily to call only two methods.
Stars: ✭ 538 (+598.7%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Swiftframeworktemplate
A template for new Swift iOS / macOS / tvOS / watchOS Framework project ready with travis-ci, cocoapods, Carthage, SwiftPM and a Readme file
Stars: ✭ 527 (+584.42%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Defaults
Swifty and modern UserDefaults
Stars: ✭ 734 (+853.25%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Openssl
OpenSSL package for SPM, CocoaPod, and Carthage, for iOS and macOS
Stars: ✭ 515 (+568.83%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Imagescout
A Swift implementation of fastimage. Supports PNG, GIF, and JPEG.
Stars: ✭ 940 (+1120.78%)
Mutual labels:  cocoapods, carthage, swiftpm
Wstagsfield
An iOS text field that represents tags, hashtags, tokens in general.
Stars: ✭ 1,013 (+1215.58%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+4718.18%)
Mutual labels:  cocoapods, carthage, swiftpm
Tweetextfield
Lightweight set of text fields with nice animation and functionality. 🚀 Inspired by https://uimovement.com/ui/2524/input-field-help/
Stars: ✭ 421 (+446.75%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Bfkit Swift
BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.
Stars: ✭ 963 (+1150.65%)
Mutual labels:  cocoapods, carthage, swift-package-manager

DIKit

Dependency Injection Framework for Swift, inspired by KOIN. Basically an implementation of service-locator pattern, living within the application's context.

Grow as you go!

We started small, it perfectly fits our use case.

Installation

Via Carthage

DIKit can be installed using Carthage. After installing Carthage just add DIKit to your Cartfile:

github "Liftric/DIKit" ~> 1.6

Via CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. After installing CocoaPods add DIKit to your Podfile:

platform :ios, '9.0'
pod 'DIKit', '~> 1.6'

Basic usage

  1. Define some sub DependencyContainer (basically some sort of module declaration):
import DIKit

public extension DependencyContainer {
    static var backend = module {
        single { Backend() as BackendProtocol }
    }
}

public extension DependencyContainer {
    static var network = module {
        single { Network() as NetworkProtocol }
    }
}

public extension DependenyContainer {
    static var app = module {
        single { AppState() as AppStateProtocol }
        factory { StopWatch() as StopWatchProtocol }
    }
}
  1. Set the root DependencyContainer and set it before the application gets initialised:
import DIKit

@UIApplicationMain
class AppDelegate: UIApplicationDelegate {
    override init() {
        super.init()
        DependencyContainer.defined(by: modules { .backend; .network; .app })
    }
}

Without sub DependencyContainer the following shorthand writing also does the job:

import DIKit

@UIApplicationMain
class AppDelegate: UIApplicationDelegate {
    override init() {
        super.init()
        DependencyContainer.defined(by: module {
            single { AppState() as AppStateProtocol }
            factory { StopWatch() as StopWatchProtocol }
        })
    }
}
  1. Inject the dependencies, for instance in a module:
import DIKit

class Backend: BackendProtocol {
    @Inject var network: NetworkProtocol
}

or a ViewController:

import DIKit

class FirstViewController: UIViewController {
    // MARK: - Dependencies
    @LazyInject var backend: BackendProtocol
    @OptionalInject var stopwatch: StopWatchProtocol?

    // MARK: - View lifecycle
    override func viewWillAppear(_ animated: Bool) {
        let result = backend.fetch()
        print(result)
    }
}

Injection via constructor:

import DIKit

struct AppState: AppStateProtocol {
    private let backend: BackendProtocol
    init(backend: BackendProtocol = resolve()) {
        self.backend = backend
    }
}

Advanced usage

Resolving by Tag

When registering your dependencies you can optionally define a tag. The tag can be anything, as long as it is AnyHashable.

This way you can register different resolvable dependencies for the same Type.

enum StorageContext: String {
    case userdata
    case systemdata
}

public extension DependencyContainer {
    static var app = module {
        factory(tag: StorageContext.systemdata) { LocalStorage() as LocalStorageProtocol }
        factory(tag: StorageContext.userdata) { LocalStorage() as LocalStorageProtocol }
    }
}

You can then reference the same tag when resolving the type and can thus resolve different instances. Referencing the tag works with all injection methods.

import DIKit

class Backend: BackendProtocol {
    @Inject(tag: StorageContext.systemdata) var injectedStorage: LocalStorageProtocol
    @LazyInject(tag: StorageContext.systemdata) var lazyInjectedStorage: LocalStorageProtocol
    @OptionalInject(tag: StorageContext.systemdata) var optionalInjectedStorage: LocalStorageProtocol?
    
    private let constructorInjectedStorage: LocalStorageProtocol
    init(storage: LocalStorageProtocol = resolve(tag: StorageContext.systemdata)) {
        self.constructorInjectedStorage = storage
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].