https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Alberto

06/30/2021, 6:48 PM
Hello, is there a way to retrieve the class name of an anonymous class? I have a usecase where I’m passing some objects from Swift to KMP, but when trying to retrieve the class name via
object::class
I only get
class <anonymous>
so calling any of the
::simpleName
or calling
getOriginalClassName
on it just returns null
e

ephemient

06/30/2021, 7:00 PM
anonymous = no name
a

Alberto

06/30/2021, 7:02 PM
makes sense, I guess I’m not too sure why the object becomes “anonymous” when in KMP
e

ephemient

06/30/2021, 7:04 PM
where is it coming from? if it's an object expression (https://kotlinlang.org/docs/object-declarations.html#object-expressions) then it should be anonymous on all platforms
a

Alberto

06/30/2021, 7:10 PM
It’s an instance of a Swift Class that I pass to a KMP class which accepts
Any
as param. Something like
class Entry(val t: Any)
then if I do
entry.t::class
returns it as anonymous
e

ephemient

06/30/2021, 7:50 PM
ah, so KClass doesn't work for ObjectiveC/Swift classes. you might need
NSStringFromClass([t class])
or equivalent. I think you can start from
Copy code
t.`class`
but I don't have macos here to test
a

Alberto

06/30/2021, 7:56 PM
that would be from Swift side right?
e

ephemient

06/30/2021, 8:07 PM
t.class should work in K/N, although the method name
class
needs to be escaped with backticks (which is difficult to render in Slack markdown). NSStringFromClass might need to be invoked from Swift or Obj-C though, I can't play around with the interop here
👍 1