objective-c 如何将 NSInteger 转换为 int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1752701/
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
How to convert An NSInteger to an int?
提问by Jeffrey
For example when passing a valuemessage to an NSIntegerinstance like so
例如,当将value消息传递给这样的NSInteger实例时
[a value]it causes an EXC_BAD_ACCESS.
[a value]它会导致 EXC_BAD_ACCESS。
So how to convert an NSIntegerto int?
那么如何将 an 转换NSInteger为int?
If it's relevant only small numbers < 32 are used.
如果相关,则仅使用 < 32 的小数字。
回答by Dave DeLong
Ta da:
达:
NSInteger myInteger = 42;
int myInt = (int) myInteger;
NSIntegeris nothing more than a 32/64 bit int. (it will use the appropriate size based on what OS/platform you're running)
NSInteger只不过是一个 32/64 位整数。(它将根据您运行的操作系统/平台使用适当的大小)
回答by Samuel Clay
If you want to do this inline, just cast the NSUIntegeror NSIntegerto an int:
如果要内联执行此操作,只需将NSUIntegeror强制NSInteger转换为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
回答by Abizern
I'm not sure about the circumstances where you need to convert an NSIntegerto an int.
我不确定您需要将 an 转换NSInteger为int.
NSInteger is just a typedef:
NSInteger 只是一个 typedef:
NSInteger Used to describe an integer independently of whether you are building for a 32-bit or a 64-bit system.
NSInteger 用于独立地描述整数,无论您是为 32 位系统还是 64 位系统构建。
#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 NSIntegerany place you use an intwithout converting it.
您可以使用NSInteger任何您使用过的地方,int而无需对其进行转换。
回答by 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):
UIsegmentedControl中常用,在64位而不是32位编译时出现“错误”,不将其传递给新变量的简单方法是使用此提示,添加(int):
[_monChiffre setUnite:(int)[_valUnites selectedSegmentIndex]];
instead of :
代替 :
[_monChiffre setUnite:[_valUnites selectedSegmentIndex]];

