Is it possible to cast or convert a `NSNumber` Obj...
# kotlin-native
j
Is it possible to cast or convert a
NSNumber
Objective-C reference to a Kotlin
Number
? The docs describe casting the other way, but not clear how to accomplish the reverse.
The best I can come up with is something like this:
Copy code
fun NSNumber.toNumber(): Number {
    return when (CFNumberGetType(this as CFNumberRef)) {
        kCFNumberSInt8Type, kCFNumberSInt16Type, kCFNumberSInt32Type, kCFNumberSInt64Type,
        kCFNumberIntType, kCFNumberCFIndexType, kCFNumberNSIntegerType -> integerValue
        kCFNumberFloat32Type, kCFNumberFloat64Type, kCFNumberFloatType, kCFNumberCGFloatType -> floatValue
        kCFNumberCharType -> charValue
        kCFNumberShortType -> shortValue
        kCFNumberLongType, kCFNumberLongLongType -> longLongValue
        kCFNumberDoubleType -> doubleValue
        else -> longLongValue
    }
}
But it doesn’t like casting 
NSNumber
 to 
CFNumberRef
. Says “this cast can never succeed”. Are toll-free bridged type casts supported?
a
I’m afraid this is not a way to make such casts. See this YouTrack issue for some details. Also, please look through the
Duplicate
section there, something might be relevant for your case.
👍 1
🙏 1