Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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