25

Possible Duplicate:
How to convert An NSInteger to an int?

I am new to iOS programming, how do I convert int to NSInteger. I have found references on how to convert NSInteger to NSNumber but I wasn't able to figure it out. Any help would be appreciated. Thanks.

Community
  • 1
  • 1
Bala
  • 2,895
  • 1
  • 19
  • 60
  • 1
    http://stackoverflow.com/questions/1752701/how-to-convert-an-nsinteger-to-an-int – Bastian Nov 22 '11 at 12:02
  • 1
    Not a dupe, I believe, at least not of the proposed original. Converting apples into apple sauce is *not* the same as trying to convert apple sauce back into an apple :-) – paxdiablo Nov 16 '15 at 04:08
  • This isn't how to turn apple sauce into apples, @paxdiablo, it's how to turn apples into _Malus domesticae_. – jscs Nov 16 '15 at 07:08
  • Why is this marked as a duplicate? Two different questions. Casting from int (32bit) to NSInt (64bit) is easy and automatic, this is asking how to cast from NSInt(64) to int(32). Re-open. – Albert Renshaw Aug 02 '18 at 02:41

1 Answers1

62

An improved answer

On 64-bit systems NSInteger is long. On 32-bit systems NSInteger is int. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Cocoa64BitGuide/64BitChangesCocoa/64BitChangesCocoa.html

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.

An original answer

int i;
NSInteger nsi = 1;
i = nsi;

No big science. :)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aleksejs Mjaliks
  • 8,647
  • 6
  • 38
  • 44