可能的重复:
何时使用 NSInteger 与 int? https://stackoverflow.com/questions/4445173/when-to-use-nsinteger-vs-int
为什么会有 NSInteger? https://stackoverflow.com/questions/5870867/why-is-there-is-an-nsinteger
我们可以使用int
and NSInteger
可以互换吗?有什么具体情况可以使用吗NSInteger
仅,而不是使用int
?
我们可以互换使用 int 和 Integer 吗?
不会。在 Apple 使用的 LP64 架构上,对于现代 OS X Cocoa,NSInteger 是 64 位宽。这意味着如果将 NSInteger 转换为 int,则与NS未找到 http://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/uid/TP40003793-CH3g-SW3可能会失败。这是一个例子:
NSRange theRange = [@"foo" rangeOfString @"x"];
int location = theRange.location;
if (location == NSNotFound) // comparison is broken due to truncation in line above
{
// x not in foo
}
在我看来,你应该只使用NSInteger
您需要将参数传递给 Cocoa 或从 Cocoa 接收结果,并且文档说数据类型是NSInteger
。在所有其他情况下:
- 如果您不关心类型的宽度,请使用 C 类型,例如
int
or long
.
- 如果您确实关心类型的宽度,请使用 C99
stdint.h
类型例如int32_t
, int64_t
.
- 如果您需要保证足够大的 int 来容纳指针,请使用
intptr_t
or uintptr_t
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)