Can't inject dependencies in iOS

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

Hello novemio,

Could you please tell us which version of Kotlin and Koin you’re currently using?

Thank you

1 Like

Hello! Koin version 3.1.5, Kotlin version 1.8.21

Hello @novemio

Sorry to get back to you this late. I just tried setting up a test KMM project with Koin 3.1.5 and Kotlin 1.8.21 on AS Giraffe. I tried following the steps in chapter 9 of the book and the app seemed to work.

Since we don’t have access to the project and setup you have, we can’t find your issue with certainty; however, here are some steps you can take to find the issue:

  1. Make sure you’re defining your modules correctly. If you forget to define the modules, Koin will fail to find and inject them.
  2. Make sure to call Koin.start() or whatever initializer you’ve defined when your iOS app starts before calling any of those Koin.instance.get(...). The best place to do so is in the init block of your App instance in Swift.
  3. When an app crashes in Xcode, it’s best to select the crash point in your own code. For instance, in the picture you attached, it’s best to select row 4 or 3 in the sidebar. The one that you selected doesn’t help that much.

I hope these tips would help.