Skip to content

KClass for Objective-C classes is not supported yet #1016

Closed
@mustafaozhan

Description

@mustafaozhan
Contributor

Describe the bug
The bug appears when you want to get a Objective-C class

Koin version 3.0.1-alpha-3

Edit: 3.1.0 remains the same
Edit: 3.1.2 remains the same

a simple example to demonstrate

// works
fun initIOS(userDefaults: NSUserDefaults) = initClient(
    module {
       single<Settings> { AppleSettings(userDefaults) }
    }
)

//fails
fun initIOS(userDefaults: NSUserDefaults) = initClient(
    module {
       single<NSUserDefaults> { userDefaults }
       single<Settings> { AppleSettings(get()) }
    }
)

It compiles fine but getting run time exception on IOS, the log below

Debug: (Kermit) starting koin
Function doesn't have or inherit @Throws annotation and thus exception isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.IllegalStateException: KClass for Objective-C classes is not supported yet
    at 0   CCC                                 0x000000010b61082f kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95
    at 1   CCC                                 0x000000010b608f8d kfun:kotlin.Exception#<init>(kotlin.String?){} + 93
    at 2   CCC                                 0x000000010b6091fd kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 93
    at 3   CCC                                 0x000000010b6098ad kfun:kotlin.IllegalStateException#<init>(kotlin.String?){} + 93
    at 4   CCC                                 0x000000010b6513cf kfun:kotlin.native.internal.KClassUnsupportedImpl#hashCode(){}kotlin.Int + 335
    at 5   CCC                                 0x000000010b8de31f kfun:org.koin.core.definition.BeanDefinition#hashCode(){}kotlin.Int + 383
    at 6   CCC                                 0x000000010b626072 kfun:kotlin.collections.HashMap.hash#internal + 226
    at 7   CCC                                 0x000000010b627736 kfun:kotlin.collections.HashMap#addKey(1:0){}kotlin.Int + 486
    at 8   CCC                                 0x000000010b6310ff kfun:kotlin.collections.HashSet#add(1:0){}kotlin.Boolean + 207
    at 9   CCC                                 0x000000010b8e77a7 kfun:org.koin.core.module#addDefinition@kotlin.collections.HashSet<org.koin.core.definition.BeanDefinition<*>>(org.koin.core.definition.BeanDefinition<*>){} + 439
    at 10  CCC                                 0x000000010b472354 kfun:com.github.mustafaozhan.ccc.client.di.initIOS$lambda-1#internal + 1252
    at 11  CCC                                 0x000000010b472541 kfun:com.github.mustafaozhan.ccc.client.di.$initIOS$lambda-1$FUNCTION_REFERENCE$52.invoke#internal + 97
    at 12  CCC                                 0x000000010b4725c1 kfun:com.github.mustafaozhan.ccc.client.di.$initIOS$lambda-1$FUNCTION_REFERENCE$52.$<bridge-UNNN>invoke(-1:0){}#internal + 97
    at 13  CCC                                 0x000000010b8fefed kfun:org.koin.dsl#module(kotlin.Boolean;kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>){}org.koin.core.module.Module + 381
    at 14  CCC                                 0x000000010b8ff184 kfun:org.koin.dsl#module$default(kotlin.Boolean;kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>;kotlin.Int){}org.koin.core.module.Module + 276
    at 15  CCC                                 0x000000010b471a82 kfun:com.github.mustafaozhan.ccc.client.di#initIOS(platform.Foundation.NSUserDefaults){}org.koin.core.KoinApplication + 354
    at 16  CCC                                 0x000000010b5aaec8 objc2kotlin.3553 + 216
    at 17  CCC                                 0x000000010b2a43d2 $s3CCC6CCCAppVACycfC + 18
    at 18  CCC                                 0x000000010b2a4bc9 $s3CCC6CCCAppV7SwiftUI3AppAadEPxycfCTW + 9
    at 19  SwiftUI                             0x000000010eebc65f $s7SwiftUI3AppPAAE4mainyyFZ + 47
    at 20  CCC                                 0x000000010b2a4b61 $s3CCC6CCCAppV5$mainyyFZ + 33
    at 21  CCC                                 0x000000010b2a4be4 main + 20
    at 22  libdyld.dylib                       0x0000000112b6895d start + 1
    at 23  ???                                 0x0000000000000001 0x0 + 1
CoreSimulator 725.10 - Device: iPhone 11 Pro Max (1C7C1A0E-2FE5-47FF-AAB1-57530F0DAD34) - Runtime: iOS 14.0 (18A5319g) - DeviceType: iPhone 11 Pro Max

Activity

Baileypollard

Baileypollard commented on Apr 8, 2021

@Baileypollard

Do we know the cause of this yet?

mustafaozhan

mustafaozhan commented on Jun 19, 2021

@mustafaozhan
ContributorAuthor

@Baileypollard yes, from the log I understand that koin doesn't support it currently. KClass for Objective-C classes is not supported yet

JohNan

JohNan commented on Aug 20, 2021

@JohNan

I have run into the same problem. My workaround for now is to create a wrapper class in kotlin.

fun iosModule(application: UIApplication) = module {
    single { UIApplicationWrapper(application) }
}

data class UIApplicationWrapper(val uiApplication: UIApplication)

Seems to work :)

slipdef

slipdef commented on Sep 2, 2021

@slipdef

Any progress on that? It blocks us to use Koin to resolve iOS dependencies.

osrl

osrl commented on Oct 18, 2021

@osrl

Is this related to what I've encountered?

I can't get from ios if the dependency is defined with interface:

single<TokenSource> {
        TokenSourceImpl()
} //this doesn't work


single {
        TokenSourceImpl()
} //this works

//kotlin function to get
fun Koin.get(objCClass: ObjCClass, parameter: Any): Any {
    val kClazz = getOriginalKotlinClass(objCClass)!!
    return get(kClazz) { parametersOf(parameter) }
}
//swift usage
lazy var tokenSource = koin.get(objCClass: TokenSourceImpl.self) as! TokenSourceImpl

Error:
Uncaught Kotlin exception: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'com.foo.main.ktor.TokenSourceImpl'. Check your definitions!

zakrodionov

zakrodionov commented on Oct 20, 2021

@zakrodionov

Same issue in Kmm

osrl

osrl commented on Oct 21, 2021

@osrl

My issue was not related to this. I needed to use ObjCProtocol instead, if anyone is interested.

mustafaozhan

mustafaozhan commented on Oct 23, 2021

@mustafaozhan
ContributorAuthor

@JohNan yes I confirm also,
It works as below

	data class NativeDependencyWrapper(val userDefaults: NSUserDefaults)
	single { NativeDependencyWrapper(userDefaults) }

and then I can use it

    AppleSettings(get<NativeDependencyWrapper>().userDefaults)

but we can not take this as solution it is just a workaround, I think koin should support this :)

mykola-dev

mykola-dev commented on Nov 8, 2021

@mykola-dev

i would really appreciate if someone explain if this is a kmm limitation or the koin library issue? just curious why we can't use ios specific dependencies directly

philhinco

philhinco commented on Jan 18, 2022

@philhinco

@osrl What do you mean with ObjCProtocol? I‘m trying to koin.get an interface that I defined in KMP and implemented in iOS, but it fails currently.

osrl

osrl commented on Jan 18, 2022

@osrl

objCProtocol instead of objCClass as parameter name

philhinco

philhinco commented on Jan 18, 2022

@philhinco

@osrl Ah, I see. And what would be the way to call it from Swift? I‘m trying to create a function or property wrapper but can‘t get it to work. The plain call to my Kotlin extension function I wrote works without problem, but I would want to make an abstraction that does the whole dance with as casting etc.

stale

stale commented on May 18, 2022

@stale

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

mustafaozhan

mustafaozhan commented on May 18, 2022

@mustafaozhan
ContributorAuthor

Still looking forward to seeing this issue fixed 🙂

15 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @JohNan@osrl@philhinco@mykola-dev@arnaudgiuliani

        Issue actions

          KClass for Objective-C classes is not supported yet · Issue #1016 · InsertKoinIO/koin