How to cast from UInt16 to NSNumber

PassKit picture PassKit · Jun 10, 2014 · Viewed 17.3k times · Source

I have a UInt16 variable that I would like to pass to a legacy function that requires an NSNumber.

If I try:

var castAsNSNumber : NSNumber = myUInt16

I get a compiler error 'UInt16' is not convertible to 'NSNumber'

Question

How can I recast this as an NSNumber?

Answer

John Estropia picture John Estropia · Jun 10, 2014
var castAsNSNumber = NSNumber(unsignedShort: myUInt16)