Skip to content

Instantly share code, notes, and snippets.

@paulz
Created March 20, 2020 21:09
Show Gist options
  • Select an option

  • Save paulz/21a1c691242d5e3ce8ea75f2930b89c6 to your computer and use it in GitHub Desktop.

Select an option

Save paulz/21a1c691242d5e3ce8ea75f2930b89c6 to your computer and use it in GitHub Desktop.
Dependency Injection For Objective C and Swift hybrid project
public struct ChainedResolver: ResolveDependency {
public func resolve<Service>(_: Service.Type, name: String?) -> Service? {
child.resolve(name: name) ?? parent.resolve(name: name)
}
let child, parent: ResolveDependency
public init(child: ResolveDependency, parent: ResolveDependency) {
self.child = child
self.parent = parent
}
}
public protocol ResolveDependency {
func resolve<Service>(_: Service.Type, name: String?) -> Service?
}
@paulz
Copy link
Copy Markdown
Author

paulz commented Mar 20, 2020

This was very helpful while migrating Objective-C code to Swift. Swift classes were able to access Objective-C dependencies without importing them via bridging headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment