The docs say that to call an Obj-C method expectin...
# multiplatform
s
The docs say that to call an Obj-C method expecting an
NSNumber
you can just cast your Kotlin number, but the IDE thinks "this cast will never succeed". Is that just a false positive? I've tried casting from
Number
,
Int
,
Double
,
UInt
, and it doesn't like any of them.
(If it is a false positive, does anyone know the magic string to
@Suppress
the warning?)
j
What about calling
.convert()
s
Hmm, I didn't know about that method before. Good to know about, but my actual variable is of type
Number
, which doesn't appear to have the method. I guess I could do
.toDouble().convert()
but that seems wasteful if the straight cast will work.
j
Is it possible that multiple of your native source sets disagree on the width of the number?
s
iOS is my only native sourceset
j
But that could have 32 and 64-bit variations of a type depending on the API you're calling
s
I'm gonna see if the cast succeeds at runtime as soon as I can write a test for this code
This is calling
NSNumberFormatter
from the iOS sourceset
expect/actual for
Number
formatting
Okay yeah the cast succeeds at runtime. The fact that it's legal is probably just not obvious to the linter.
After some Googling,
@Suppress("CAST_NEVER_SUCCEEDS")
satisfies my desire to be rid of the false warning 🙂
l
If the size varies, it likes to use size_t, which gets expect/actual instead of Number. Adding the suppress is fine. You'll also need it to cast String to NSString. I'd add a comment to the suppress saying this is defined by the compiler as safe, and do it per-line so it doesn't suppress any actual issues.