Converting int to NSInteger

Bala picture Bala · Nov 22, 2011 · Viewed 58.7k times · Source

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.

Answer

Aleksejs Mjaliks picture Aleksejs Mjaliks · Nov 22, 2011

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. :)