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
Converting int to NSInteger
提问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 NSInteger
is long
. On 32-bit systems NSInteger
is int
.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Cocoa64BitGuide/64BitChangesCocoa/64BitChangesCocoa.html
在 64 位系统上NSInteger
是long
. 在 32 位系统上NSInteger
是int
.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Cocoa64BitGuide/64BitChangesCocoa/64BitChangesCocoa.html
All you need just cast your int
to NSInteger
.
您只需要将您的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.
您也可以强制转换NSInteger
为int
(如原始答案),但在 64 位系统上必须小心,因为您NSInteger
可以超过int
限制。
An original answer
一个原始答案
int i;
NSInteger nsi = 1;
i = nsi;
No big science. :)
没有什么大科学。:)