0

So I'm trying to check whether NSNumber is Double or Int and I'm wondering if it's even possible.

I was hoping this will work, but it always returns true no matter with which type I'm comparing

var myDouble: Double = 0.0
var myNSNumber: NSNumber = NSNumber(value: myDouble)

if myNSNumber is Double {
    print("NSNumber is Double")
}else {
    print("NSNumber is different type")
}

In kotlin I'm using such Extension for Number which is NSNumber in swift and I want to recreate it in swift

protected operator fun Number.plus(other: Number): Number {
    return when (this) {
        is Long -> this.toLong() + other.toLong()
        is Int -> this.toInt() + other.toInt()
        is Short -> this.toShort() + other.toShort()
        is Byte -> this.toByte() + other.toByte()
        is Double -> this.toDouble() + other.toDouble()
        is Float -> this.toFloat() + other.toFloat()
    }
}
Amal T S
  • 3,327
  • 2
  • 24
  • 57
LeTadas
  • 3,436
  • 9
  • 33
  • 65
  • What do *you* mean by "is Double"? Do you actually want to check wether the value inside is properly representable as a `Double` or do you want to check wether the value that was used to create the `NSNumber` was originally a `Double`? – luk2302 May 24 '19 at 12:07
  • I want to check wether the value that was used to create the NSNumber was originally a Double – LeTadas May 24 '19 at 12:08
  • May I ask: why? That must not matter to you unless you are doing some very weird things. – luk2302 May 24 '19 at 12:09
  • What @luk2302 said. Why not just call `myNSNumber.doubleValue` if you need a Double from an NSNumber? – Mark Thormann May 24 '19 at 12:09
  • Compare https://stackoverflow.com/q/20198040/1187415. – Martin R May 24 '19 at 12:10
  • check update on question – LeTadas May 24 '19 at 12:12
  • 1
    Maybe it's better you explained what you are trying to achieve with this, there might be a different way to do it rather than using NSNumber. I am also curious, in your kotlin code 3 + 3.5 = 6, is this intended behavior? – Joakim Danielson May 24 '19 at 12:49

2 Answers2

1

You can get type of the number stored using low-level CoreFoundation API:

extension NSNumber {
    var type: CFNumberType {
        return CFNumberGetType(self as CFNumber)
    }
}

And then you can use it.

Your plus function gonna be something like that:

extension NSNumber {
    func plus(other: NSNumber) -> NSNumber {
        switch type {
        case .sInt8Type, .charType:
            return NSNumber(value: self.int8Value + other.int8Value)
        case .sInt16Type, .shortType:
            return NSNumber(value: self.int16Value + other.int16Value)
        case .sInt32Type, .longType:
            return NSNumber(value: self.int32Value + other.int32Value)
        case .sInt64Type, .longLongType:
            return NSNumber(value: self.int64Value + other.int64Value)
        case .float32Type, .floatType:
            return NSNumber(value: self.floatValue + other.floatValue)
        case .float64Type, .doubleType:
            return NSNumber(value: self.doubleValue + other.doubleValue)
        case .intType, .cfIndexType, .nsIntegerType:
            return NSNumber(value: self.intValue + other.intValue)
        case .cgFloatType:
            switch MemoryLayout<CGFloat>.size {
            case 4:
                return NSNumber(value: self.floatValue + other.floatValue)
            default:
                return NSNumber(value: self.doubleValue + other.doubleValue)
            }
        }
    }
}
user28434'mstep
  • 6,290
  • 2
  • 20
  • 35
-1

Try this code.

var myDouble: Double = 0.0
var myNSNumber: NSNumber = NSNumber(value: myDouble)

if myNSNumber.isKindOfClass(Double) {
    print("NSNumber is Double")
} else {
    print("NSNumber is different type")
}
Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28