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.
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With