Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert An NSInteger to an int?

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.

like image 764
Jeffrey Avatar asked Nov 17 '09 23:11

Jeffrey


People also ask

Is NSNumber integer?

NSNumber provides readonly properties that return the object's stored value converted to a particular Boolean, integer, unsigned integer, or floating point C scalar type.

How do I create an NSInteger in Objective C?

All you need just cast your int to NSInteger . int i = 1; NSInteger nsi = (NSInteger) i; You can also cast NSInteger to int (as in an original answer), but you must be careful on 64-bit system, because your NSInteger can exceed int limits.


2 Answers

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)

like image 74
Dave DeLong Avatar answered Sep 20 '22 10:09

Dave DeLong


If you want to do this inline, just cast the NSUInteger or NSInteger to an int:

int i = -1; NSUInteger row = 100; i > row // true, since the signed int is implicitly converted to an unsigned int i > (int)row // false 
like image 22
Samuel Clay Avatar answered Sep 22 '22 10:09

Samuel Clay