Waffle 1.0.5

Waffle 1.0.5

LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2017
SwiftSwift Version 3.1
SPMSupports SPM

Maintained by Jakob Jensen.

Waffle 1.0.5

  • By
  • Jakob Jensen

Waffle is a deadsimple dependency container. Waffle does not try to do too much, it will meet between 80 to 100 percent of your needs and allows you to extend Waffle so you can match all of your needs.

Basic Usage

Suppose you have the following two datasources.

struct UserDataSource {
    let name:String
struct RestDataSource {
    let userDataSource:UserDataSource

    func sayHi() {
        print("Hi, \(userDataSource.name)")

First, create a builder that you add all your dependencies to and let it build your Waffle which holds them all.

let userDataSource = UserDataSource(name:"Mr. Example")
let restDataSource = RestDataSource(userDataSource: userDataSource)

let waffle = Waffle.Builder()

You pass the waffle on to what ever class has dependencies, where you can retrieve its dependencies like the following few lines.

let restDataSource = try! waffle.get(RestDataSource.self)
restDataSource.sayHi() // prints "Hi, Mr. Example"

This means you can have a constructor like in the following example.

class RestDependentViewController : UIViewController {
	private unowned let restDataSource: RestDataSource
	init(waffle:Waffle) {
		restDataSource = try! waffle.get(RestDataSource.self)
	// ...

Simple is beautiful, right?

Dealing with generics

There is a potential issue when using Generics, but do not worry there is an easy fix for it too. Here is an example of how generics can be a problem.

let cache = NSCache<AnyObject, AnyObject>()
let waffle = Waffle.Builder()
    .add(NSCache<NSString, AnyObject>())

// NOTE: this next line fails
let resolvedCache = try! waffle.get(NSCache<AnyObject, AnyObject>.self) // throws .multipleFound

The reason it fails is that the Type of both of the NSCaches that were added to the waffle is simply NSCache and but e.g. NSCache<AnyObject, AnyObject>.

The workaround is simple enough, you just create a named class instead. Here is an example how to work around the generics issue.

class NamedCache : NSCache<AnyObject, AnyObject> { }
let cache = NamedCache()
let waffle = Waffle.Builder()
    .add(NSCache<NSString, AnyObject>())

let resolvedCache = try! waffle.get(NamedCache.self) // this does not throw an error


Why did you make Waffle?
I did not understand why all the other dependency managers/injectors were bloated, so I felt like making an alternative solution.
Why is it named 'Waffle'?
Waffles are delicous and I am craving one right now.