Skip to content

Instantly share code, notes, and snippets.

@hishd
Created March 27, 2024 06:32
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 hishd/9e64afb70cb095167785dac91c0a4d75 to your computer and use it in GitHub Desktop.
Save hishd/9e64afb70cb095167785dac91c0a4d75 to your computer and use it in GitHub Desktop.
//
// DependencyInjector.swift
// SkyWizard
//
// Created by Hishara Dilshan on 2023-10-10.
//
import Foundation
protocol Injectable {}
@propertyWrapper
/// Usage: @Inject var propertyName: Type (that conforms to Injectable)
struct Inject<T: Injectable> {
var wrappedValue: T
init() {
self.wrappedValue = Resolver.shared.resolve()
}
}
class Resolver {
static let shared = Resolver()
private var storage: [String: Injectable] = [:]
private init() {}
func add<T: Injectable>(injectable: T) {
let key = String(reflecting: injectable)
storage[key] = injectable
}
func resolve<T: Injectable>() -> T {
let key = String(reflecting: T.self)
guard let injectable = storage[key] as? T else {
fatalError("Injectable not found on storage")
}
return injectable
}
}
class DependencyManager {
//let dependency: Type (conforms to Injectable)
init() {
//Resolver.shared.add(dependency)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment