将int转换为NSInteger

将int转换为NSInteger

问题描述:


可能重复:

如何将NSInteger转换为int?

我是iOS编程新手,如何将int转换为NSInteger。我找到了关于如何将NSInteger转换为NSNumber的参考资料,但我无法弄明白。任何帮助,将不胜感激。谢谢。

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.

改进的答案



在64位系统上 NSInteger long 。在32位系统上 NSInteger int
https://developer.apple .com / library / content / documentation / Cocoa / Conceptual / Cocoa64BitGuide / 64BitChangesCocoa / 64BitChangesCocoa.html

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

您只需要投出 int to NSInteger

All you need just cast your int to NSInteger.

int i = 1;
NSInteger nsi = (NSInteger) i;

您还可以将 NSInteger 投射到 int (如原始答案中所述),但您必须小心64位系统,因为 NSInteger 可能超过 int 限制。

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.

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

没有大科学。 :)