ChatGPT解决这个技术问题 Extra ChatGPT

How to convert An NSInteger to an int?

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.

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.

c
cdub

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)


To be precise, I think NSInteger is an int on 32-bit platforms, and a long on 64-bit platforms.
I get a warning using the suggested "casting": Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int'
@djcj, yes, answer is not correct for new SDKs. I think.
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 why are you trying to avoid a new variable?
S
Samuel Clay

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

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.
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.
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.
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.
And I stumbled on this page yet another year and a half later, needing the NSUInteger-related cast specifically. Thanks, Samuel!
A
Abizern

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.


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.
@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.
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.
g
grominet

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]];