Skip to content

Instantly share code, notes, and snippets.

@aratkevich
Created January 7, 2018 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aratkevich/8d0cfaed6f10193d583ab2a9b1127db8 to your computer and use it in GitHub Desktop.
Save aratkevich/8d0cfaed6f10193d583ab2a9b1127db8 to your computer and use it in GitHub Desktop.
ServiceLocating
protocol ServiceLocating {
func getService<T>() -> T?
}
final class ServiceLocator: ServiceLocating {
private lazy var services: Dictionary<String, Any> = [:]
private func typeName(some: Any) -> String {
return (some is Any.Type) ?
"\(some)" : "\(some.dynamicType)"
}
func addService<T>(service: T) {
let key = typeName(T)
services[key] = service
}
func getService<T>() -> T? {
let key = typeName(T)=
return services[key] as? T
}
public static let shared: ServiceLocator()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment