Search code examples
swiftlinuxubuntuserver-side-swift

Numeric types don't automatically bridge to NSNumber in pure Swift on Ubuntu Linux?


On MacOS, if you do:

import Foundation
let x = Int32(1) as? NSNumber

x is non-nil.

On Ubuntu Linux, if you do the same (even with Swift 3.1.1), x is nil.

Am I doing something wrong, or is this just a fact of a lack of bridging of numeric types to NSNumber with the Foundation with Swift on Ubuntu/Linux?

See also Is it possible to replicate Swifts automatic numeric value bridging to Foundation (NSNumber) for (U)Int8/16/32/64 types? and https://github.com/SwiftyJSON/SwiftyJSON/issues/745


Solution

  • There is no automatic bridging to NSNumber on Linux. Source: NSNumber bridging and Numeric types:

    Considerations for Linux platforms

    We do not have bridging on Linux so the as? cast is less important; but if it were to have bridging this would be the desired functionality.