ios 将 int 转换为 NSInteger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8226447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:31:33  来源:igfitidea点击:

Converting int to NSInteger

iphoneiosintegernsinteger

提问by Bala

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

可能的重复:
如何将 NSInteger 转换为 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.

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

回答by Aleksejs Mjaliks

An improved answer

改进的答案

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

在 64 位系统上NSIntegerlong. 在 32 位系统上NSIntegerint. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Cocoa64BitGuide/64BitChangesCocoa/64BitChangesCocoa.html

All you need just cast your intto NSInteger.

您只需要将您的intto NSInteger.

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

You can also cast NSIntegerto int(as in an original answer), but you must be careful on 64-bit system, because your NSIntegercan exceed intlimits.

您也可以强制转换NSIntegerint(如原始答案),但在 64 位系统上必须小心,因为您NSInteger可以超过int限制。

An original answer

一个原始答案

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

No big science. :)

没有什么大科学。:)