I’m trying to use an objective-c class that expect...
# kotlin-native
b
I’m trying to use an objective-c class that expects a
Class<SomeProtocol>
that it can internally instantiate when needed. In kotlin I can create a class that conforms to
SomeProtocol
- but I don’t know how to (or if it’s even possible to) get an “Objective-C class” from that class. I tried passing it as
MyClass::class
, but the compiler complains that it got a
KClass<MyClass>
but was expecting a
SomeProtocol?
- which isn’t quite right, because the API I’m calling expects a class, not an instance. Using
MyClass::class as SomeProtocol
appeases the compiler, but results in a TypeCastException at runtime. Is there anything analogous to
KotlinClass::class.java
in the native world?
a
b
Thanks for the link - that resolved most of my issue. With the companion object, I can now treat my class object as an objective-c
Class
type. Unfortunately, I couldn't figure out how to finish things in kotlin - the compiler still complains that it is expecting a
SomeProtocol?
, but now I'm passing in an
ObjCClass
. I couldn't find any cast that works (and I'm not even sure how to represent that type in kotlin, an
ObjCClass
whose instances conform to
SomeProtocol
). I ended up adding an objective-c .def file with a function that takes in a
Class
and then passes that through to the API that expects a
Class<SomeProtocol>
. Not as clean as I'd hoped, but it works, and it's a simple workaround.