How to convert An NSInteger to an int?
165,709
Solution 1
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)
Solution 2
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
Solution 3
I'm not sure about the circumstances where you need to convert an NSInteger
to an int
.
NSInteger is just a typedef:
NSInteger Used to describe an integer independently of whether you are building for a 32-bit or a 64-bit system.
#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif
You can use NSInteger
any place you use an int
without converting it.
Related videos on Youtube
Author by
Jeffrey
Updated on February 17, 2020Comments
-
Jeffrey over 4 years
For example when passing a
value
message to anNSInteger
instance like so[a value]
it causes an EXC_BAD_ACCESS.So how to convert an
NSInteger
toint
?If it's relevant only small numbers < 32 are used.
-
Mike Abdullah over 14 yearsYou seem to be rather confused. [a value] suggests you expect a to be an object, but that its an NSInteger at the moment. "Converting" to an int will not solve that problem.
-
-
Samuel Clay over 12 yearsHere's a good reason to want to convert a NSUinteger to an int: value comparisons. (int i = -1) > (NSUinteger j = 14) converts the int to unsigned, meaning -1 > 14, which is not what you want.
-
Lily Ballard over 12 yearsJust out of curiosity, why are you posting an answer 2 years after the fact on a question that already has an accepted answer with votes in the double digits? Especially since yours is just a rephrasing of that answer.
-
Abizern over 12 years@SamuelClay. A good point when used in specific cases. Personally, I'd convert a NSUInteger to a uint. However. The question was about converting NSInteger, not NSUInteger.
-
Samuel Clay over 12 yearsBecause my answer is a single line (using an inline type cast -- the (int)), which I think is what the OP might want. I looked into this myself and noticed all of the answers on the subject were multi-line.
-
Samuel Clay over 12 yearsI only used an unsigned int to illustrate a common pitfall. It's the same type cast for both, as my answer demonstrates. It's not a difficult problem, but it's common enough and I have yet to see anybody recommend a single-line answer.
-
Lily Ballard over 12 yearsThe accepted answer demonstrates an implicit conversion using a single line. Abizern's answer explains that an implicit conversion will happen if you try to use an
NSInteger
value in place of anint
. In neither case is it a multi-line solution. -
Samuel Clay over 12 yearsI should've been clearer. Since this is a somewhat novice question, it may not be obvious to the novice programmer that they can convert inline with an (int) expression. The other answers don't show this and I think it helps anybody who stumbles on this page.
-
Frederic Adda over 10 yearsTo be precise, I think NSInteger is an int on 32-bit platforms, and a long on 64-bit platforms.
-
leanne about 10 yearsAnd I stumbled on this page yet another year and a half later, needing the NSUInteger-related cast specifically. Thanks, Samuel!
-
djcj almost 10 yearsI get a warning using the suggested "casting": Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int'
-
Nazik over 9 years@djcj, yes, answer is not correct for new SDKs. I think.
-
chandhooguy over 9 yearsWould it not be much easier to just put (int) in front of the NSInteger? For example, (int)myInteger whenever you want to call the integer form, so that you do not have to create a new variable.
-
Dan Rosenstark about 9 years@chandhooguy why are you trying to avoid a new variable?
-
John almost 6 yearsThe most popular answer is a single line: (int) myInteger - Amazing that we need three different data types for an integer!