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

Sam

03/22/2023, 10:35 PM
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

jw

03/22/2023, 10:42 PM
What about calling
.convert()
s

Sam

03/22/2023, 10:44 PM
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

jw

03/22/2023, 10:46 PM
Is it possible that multiple of your native source sets disagree on the width of the number?
s

Sam

03/22/2023, 10:46 PM
iOS is my only native sourceset
j

jw

03/22/2023, 10:46 PM
But that could have 32 and 64-bit variations of a type depending on the API you're calling
s

Sam

03/22/2023, 10:47 PM
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

Landry Norris

03/23/2023, 1:39 PM
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.
5 Views