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.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Jeffrey
  • 1,392
  • 4
  • 11
  • 18
  • 2
    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. – Mike Abdullah Nov 18 '09 at 11:23

4 Answers4

213

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)

cdub
  • 24,555
  • 57
  • 174
  • 303
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 13
    To be precise, I think NSInteger is an int on 32-bit platforms, and a long on 64-bit platforms. – Frederic Adda Mar 11 '14 at 22:16
  • 5
    I get a warning using the suggested "casting": Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int' – djcj Sep 18 '14 at 16:52
  • @djcj, yes, answer is not correct for new SDKs. I think. – Nazik Nov 29 '14 at 15:12
  • 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. – chandhooguy Dec 28 '14 at 22:05
  • 2
    @chandhooguy why are you trying to avoid a new variable? – Dan Rosenstark May 07 '15 at 15:34
28

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
Samuel Clay
  • 1,252
  • 2
  • 13
  • 24
  • 1
    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. – Lily Ballard Jan 03 '12 at 23:29
  • 6
    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. – Samuel Clay Jan 04 '12 at 20:57
  • 2
    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. – Lily Ballard Jan 04 '12 at 21:24
  • 25
    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. – Samuel Clay Jan 17 '12 at 01:07
  • 4
    And I stumbled on this page yet another year and a half later, needing the _NSUInteger_-related cast specifically. Thanks, Samuel! – leanne Apr 25 '14 at 03:33
  • The most popular answer is a single line: (int) myInteger - Amazing that we need three different data types for an integer! – John Aug 22 '18 at 17:45
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.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 2
    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. – Samuel Clay Jan 03 '12 at 23:12
  • 1
    @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. – Abizern Jan 04 '12 at 01:52
  • 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. – Samuel Clay Jan 04 '12 at 20:59
0

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]];
grominet
  • 274
  • 1
  • 3
  • 13