AliSoftware / Dip

Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!
MIT License
978 stars 75 forks source link

Injecting SwiftyBeaver as logger #163

Closed orbitbot closed 7 years ago

orbitbot commented 7 years ago

I'm new to iOS & Swift in general, and am in the process of setting up a new project with Dip & Dip-Ui and most things according to the documentation has been going well. However, we're using SwiftyBeaver for logging in the application, and their documentation suggests setting up a global log variable like so:

import SwiftyBeaver
let log = SwiftyBeaver.self

While convenient, this also means that I need to define the same global in my unit test setups, which feels like it may cause issues. I would rather use autoInjection to provide this dependency on a per file & per test basis, but I have not been able to add something written as the SwiftyBeaver class to my container. As the linked file shows, this class does not have an explicit initializer and uses class functions for functionality.

Is there any way to "wrap" this kind of class?

ilyapuchka commented 7 years ago

Just register your logger like container.register { SwiftyBeaver.self } and resolve it with container.resolve() as SwiftyBeaver.Type, with auto-injected property it will belet logger = Injected<SwiftyBeaver.Type>()

orbitbot commented 7 years ago

Thanks for the reply. That was one of the permutations that I have already tried, it fails in compilation trying to use methods (logger.debug("sth") with

Value of type 'Injected<SwiftyBeaver.Type>' has no member 'debug'

However, did just notice that logger.value seems to have the correct methods, is this an autoinject issue or something related to the Swift "Type" (excuse my ignorance)?

ilyapuchka commented 7 years ago

That's how auto-injected properties work, you have to use value to access underlying injected instance.

orbitbot commented 7 years ago

OK, thanks, I guess I misread the documentation a bit.