How to convert An NSInteger to an int?
Asked Answered
A

4

101

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.

Aldarcie answered 17/11, 2009 at 23:50 Comment(1)
You 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.Reconnoiter
P
214

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)

Pentheas answered 17/11, 2009 at 23:54 Comment(5)
To be precise, I think NSInteger is an int on 32-bit platforms, and a long on 64-bit platforms.Howlet
I get a warning using the suggested "casting": Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int'Pendergrass
@djcj, yes, answer is not correct for new SDKs. I think.Schnitzel
Would 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.Teetotum
@Teetotum why are you trying to avoid a new variable?Categorize
W
27

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
Wildfire answered 3/1, 2012 at 23:23 Comment(6)
Just 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.Rye
Because 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.Wildfire
The 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 an int. In neither case is it a multi-line solution.Rye
I 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.Wildfire
And I stumbled on this page yet another year and a half later, needing the NSUInteger-related cast specifically. Thanks, Samuel!Corruptible
The most popular answer is a single line: (int) myInteger - Amazing that we need three different data types for an integer!Swanky
P
18

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.

Paid answered 18/11, 2009 at 2:38 Comment(3)
Here'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.Wildfire
@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.Paid
I 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.Wildfire
N
-1

Commonly used in UIsegmentedControl, "error" appear when compiling in 64bits instead of 32bits, easy way for not pass it to a new variable is to use this tips, add (int):

[_monChiffre setUnite:(int)[_valUnites selectedSegmentIndex]];

instead of :

[_monChiffre setUnite:[_valUnites selectedSegmentIndex]];
Nicolella answered 12/5, 2014 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.