ppalancica ppalancica - 1 year ago 124
iOS Question

How to convert Int to int in swift?

I have a variable called number of type Int

var number = value!.integerValue as Int

Now I have to create a NSNumber object using that value.

I am trying to use this constructor

value = NSNumber(int: number)

, but it does not work.

It expect the primitive type int, not Int I guess.

Anyone know how to get around this?


Answer Source

You just do value = number

As you can see in the documentation:


the native swift number types generally bridge directly to NSNumber.


Swift automatically bridges certain native number types, such as Int and Float, to NSNumber. This bridging lets you create an NSNumber from one of these types:


let n = 42
let m: NSNumber = n
It also allows you to pass a value of type Int, for example, to an argument expecting an NSNumber. Note that because NSNumber can contain a variety of different types, you cannot pass it to something expecting an Int value.

All of the following types are automatically bridged to NSNumber:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download