Hello, I am building a KMM project and am using Koin for DI. Everything works fine on Android
I just want to get the repository inside the viewModelWrapper on iSO. There I use
let repository: Repository = Koin.instance.get()
in order to get the repository from Koin
In iOSMain, by following the tutorial I have KoinIOS.kt
`
import com.russhwolf.settings.NSUserDefaultsSettings
import com.russhwolf.settings.Settings
import kotlinx.cinterop.ObjCClass
import kotlinx.cinterop.getOriginalKotlinClass
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.Qualifier
import org.koin.dsl.module
import platform.Foundation.NSUserDefaults
object KoinIOS {
fun initialize(
userDefaults: NSUserDefaults,
): KoinApplication = initKoin(
appModule = module {
single {
NSUserDefaultsSettings(userDefaults)
}
}
) // swift doesn’t bridge Kotlin functions with default parameters. This functions is to compensate for that limitation
}
fun Koin.get(objCClass: ObjCClass): Any {
val kClazz = getOriginalKotlinClass(objCClass)!!
return get(kClazz, null, null)
}
fun Koin.get(objCClass: ObjCClass, qualifier: Qualifier?, parameter: Any): Any {
val kClazz = getOriginalKotlinClass(objCClass)!!
return get(kClazz, qualifier) { parametersOf(parameter) }
}
actual val platformModule = module {}
`
I iOSApp
there is Koin.swift
with the following:
`
import shared
final class Koin {
private var core: Koin_coreKoin?
static let instance = Koin()
static func start() {
if instance.core == nil {
let app = KoinIOS.shared.initialize(
userDefaults: UserDefaults.standard
)
instance.core = app.koin
}
if instance.core == nil {
fatalError(“Can’t initialize Koin.”)
}
}
private init() {
}
func get<T: AnyObject>() → T {
guard let core = core else {
fatalError(“You should call start()
before using (#function)”)
}
guard let result = core.get(objCClass: T.self) as? T else {
fatalError("Koin can't provide an instance of type: \(T.self)")
}
return result
}
}
`
Error I am getting at the moment is when I call Koin.instance.get()
from iOSMain
and then it tries to execute
guard let result = core.get(objCClass: T.self) as? T else {fatalError("Koin can't provide an instance of type: \(T.self)") }
Error is
Thread 1: EXC_BAD_ACCESS (code=1, address=0x310)
Here is the screenshot of the error I get on XCode