Search code examples
swift

Convert a double to NSNumber in Swift


How do I convert a double to NSNumber in Swift?

I tried

let myDouble = 7.12
NSNumber(myDouble)

But got the error:

Argument labels '(_:)' do not match any available overloads


Solution

  • Use init(value:)

    let nsNumber = NSNumber(value: myDouble)
    

    or simply a cast

    let nsNumber = myDouble as NSNumber