How to convert An NSInteger to an int?

Jeffrey picture Jeffrey · Nov 18, 2009 · Viewed 160.9k times · Source

For example when passing a value message to an NSInteger instance like so

[a value] it causes an EXC_BAD_ACCESS.

So how to convert an NSInteger to int?

If it's relevant only small numbers < 32 are used.

Answer

Dave DeLong picture Dave DeLong · Nov 18, 2009

Ta da:

NSInteger myInteger = 42;
int myInt = (int) myInteger;

NSInteger is nothing more than a 32/64 bit int. (it will use the appropriate size based on what OS/platform you're running)