Created
September 16, 2019 14:07
-
-
Save pixelrevision/b884ed5ce235e7b1a88772e294fefbba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DependencyContainer { | |
static let shared = DependencyContainer() | |
private var types: [String: Any] = [:] | |
func registerType<T: Any>(_ type: T.Type, named name: String? = nil, creator: @escaping (String) -> T) { | |
let key = name ?? String(describing: type) | |
types[key] = creator | |
} | |
func resolveType<T>(_ type: T.Type, named name: String? = nil) -> T? { | |
let key = name ?? String(describing: type) | |
guard let creator = types[key] as? (String) -> T else { | |
return nil | |
} | |
return creator(key) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment