Numeric types don't automatically bridge to NS

2020-07-18 07:21发布

问题:

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

回答1:

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.