anyone here use Koin for multiplatform projects? I...
# multiplatform
a
anyone here use Koin for multiplatform projects? I'm doing some KMM prototyping and am woundering if I can use Koin to inject in my Swift code
w
We use, so far it works.
a
what does injection look like in swift code? I can't find any examples
w
Okay-ish:
Copy code
DependencyListDirectory.default.get(IsAuthenticatedUseCase.self)
That
get
is our function:
Copy code
func get<T>(_ type: T.Type) -> T {
        sharedDependencyList.get(objCObject: type) as! T
    }

    func get<T>(_ type: T.Type, qualifier: Qualifier?) -> T {
        sharedDependencyList.get(objCObject: type, qualifier: qualifier) as! T
    }
Which calls in the
Koin
instance:
private var sharedDependencyList: Koin!
And that
get
in Koin is our own extension defined in the
ios
sourceset in Kotlin, as to retrieve the Kotlin class:
Copy code
// region Helper functions to retrieve a Koin dependency from Swift code.
fun Koin.get(objCObject: ObjCObject): Any {
    return get(objCObject, null)
}

fun Koin.get(objCObject: ObjCObject, qualifier: Qualifier?): Any {
    val kClazz = when (objCObject) {
        is ObjCClass -> getOriginalKotlinClass(objCObject)!!
        is ObjCProtocol -> getOriginalKotlinClass(objCObject)!!
        else -> {
            throw IllegalArgumentException("Koin.get objObject must be a class or a protocol")
        }
    }
    return get(kClazz, qualifier, null)
}
// endregion.
a
awesome, thanks!
👍 1
so
DependencyListDirectory
is a global you set some where to Koin's instance?
w
yes, is from the Koin method
startKoin
, which returns the
KoinApplication
and then we store the
koinApplication.koin
a
great, thanks for clearing that up
w
👍 I touched a little in our blog post: https://medium.com/clearscore/drivescore-kmm-journey-episode-iii-issues-and-solutions-a83f58234b2 There I focused in the interop with Dagger, but shows a little there in how we start Koin in both platforms too. And other issues we had, in case you face similar things.
a
oh wow great, ya we're some some prototyping right now at work, evaluating KMM, the KMM/Android story has been pretty good (compared to when I tried it about a year ago) but the KMM/iOS story has been less smooth so far