Last active
July 9, 2019 22:49
-
-
Save fromkk/ce9e92f6b1419bf21078208ba70545a5 to your computer and use it in GitHub Desktop.
Simplest DI container in Swift
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
public class DIContainer { | |
public typealias Resolver = ([Any]) -> Any? | |
static var resolvers: [ObjectIdentifier: Resolver] = [:] | |
public static func register<T>(_ type: T.Type, resolver: @escaping Resolver) { | |
resolvers[ObjectIdentifier(type)] = resolver | |
} | |
public static func resolve<T>(_ type: T.Type, args: Any...) -> T? { | |
guard let resolver = resolvers[ObjectIdentifier(type)] else { return nil } | |
return resolver(args) as? T | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage