objective-c 在 Cocoa 中,你更喜欢 NSInteger 还是 int,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13725/
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
In Cocoa do you prefer NSInteger or int, and why?
提问by Andrew Grant
NSInteger/NSUIntegerare Cocoa-defined replacements for the regular built-in types.
NSInteger/NSUInteger是 Cocoa 定义的常规内置类型的替代品。
Is there any benefit to using the NS* types over the built-ins? Which do you prefer and why? Are NSIntegerand intthe same width on 32-bit / 64-bit platforms?
与内置函数相比,使用 NS* 类型有什么好处吗?你更偏向于哪个,为什么?NSInteger和int32 位 / 64 位平台上的宽度是否相同?
回答by Theo
The way I understand it is that NSInteger et al. are architecture safe versions of the corresponding C types. Basically their size vary depending on the architecture, but NSInteger, for example, is guaranteed to hold any valid pointer for the current architecture.
我理解的方式是 NSInteger 等人。是相应 C 类型的体系结构安全版本。基本上它们的大小因架构而异,但例如,NSInteger 保证保存当前架构的任何有效指针。
Apple recommends that you use these to work with OS X 10.5 and onwards, and Apple's API:s will use them, so it's definitely a good idea to get into the habit of using them. They require a little more typing, but apart from that it doesn't seem to be any reason not to use them.
Apple 建议您将它们用于 OS X 10.5 及更高版本,Apple 的 API:s 将使用它们,因此养成使用它们的习惯绝对是个好主意。它们需要更多的输入,但除此之外,似乎没有任何理由不使用它们。
回答by mmalc
Quantisation issues for 64-bit runtime
64 位运行时的量化问题
In some situations there may be good reason to use standard types instead of NSInteger: "unexpected" memory bloat in a 64-bit system.
在某些情况下,可能有充分的理由使用标准类型而不是NSInteger: 64 位系统中的“意外”内存膨胀。
Clearly if an integer is 8 instead of 4 bytes, the amount of memory taken by values is doubled. Given that not every value is an integer, though, you should typically not expect the memory footprint of your application to double. However, the way that Mac OS X allocates memory changes depending on the amount of memory requested.
很明显,如果一个整数是 8 个而不是 4 个字节,值所占用的内存量会增加一倍。不过,鉴于并非每个值都是整数,您通常不应期望应用程序的内存占用量会翻倍。但是,Mac OS X 分配内存的方式会根据请求的内存量而变化。
Currently, if you ask for 512 bytes or fewer, mallocrounds up to the next multiple of 16 bytes. If you ask for more than 512 bytes, however, mallocrounds up to the next multiple of 512 (at least 1024 bytes). Suppose then that you define a class that -- amongst others -- declares five NSIntegerinstance variables, and that on a 32-bit system each instance occupies, say, 272 bytes. On a 64-bit system, instances would in theory require 544 bytes. But, because of the memory allocation strategy, each will actually occupy 1024 bytes (an almost fourfold increase). If you use a large number of these objects, the memory footprint of your application may be considerably greater than you might otherwise expect. If you replaced the NSIntegervariables with sint_32variables, you would only use 512 bytes.
目前,如果您要求 512 字节或更少,则malloc向上舍入到下一个 16 字节的倍数。但是,如果您要求超过 512 个字节,则malloc向上舍入到 512 的下一个倍数(至少 1024 个字节)。假设您定义了一个类,其中声明了五个NSInteger实例变量,并且在 32 位系统上,每个实例占用,比如说,272 个字节。在 64 位系统上,实例理论上需要 544 字节。但是,由于内存分配策略,每个实际上会占用 1024 字节(几乎增加了四倍)。如果您使用大量这些对象,您的应用程序的内存占用可能比您预期的要大得多。如果NSInteger用sint_32变量替换变量,则将仅使用 512 个字节。
When you're choosing what scalar to use, therefore, make sure you choose something sensible. Is there any reason why you need a value greater than you needed in your 32-bit application? Using a 64-bit integer to count a number of seconds is unlikely to be necessary...
因此,当您选择要使用的标量时,请确保选择合理的标量。是否有任何理由需要一个大于 32 位应用程序所需的值?不太可能需要使用 64 位整数来计算秒数......
回答by S?ren Kuklau
64-bit is actually the raison d'être for NSInteger and NSUInteger; before 10.5, those did not exist. The two are simply defined as longs in 64-bit, and as ints in 32-bit:
64 位实际上是 NSInteger 和 NSUInteger 存在的理由;在 10.5 之前,这些都不存在。这两者在 64 位中被简单地定义为 long,在 32 位中被定义为 int:
#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
Thus, using them in place of the more basic C types when you want the 'bit-native' size.
因此,当您想要“位本机”大小时,使用它们代替更基本的 C 类型。
回答by JamesSugrue
I prefer the standard c style declarations but only because I switch between several languages and I don't have to think too much about it but sounds like I should start looking at nsinteger
我更喜欢标准的 c 风格声明,但这只是因为我在多种语言之间切换,我不必考虑太多,但听起来我应该开始研究 nsinteger
回答by Jason Fuerstenberg
For importing and exporting data to files or over the net I use UInt32, SInt64etc...
为了将数据导入和导出到文件或通过网络,我使用UInt32、SInt64等...
These are guaranteed to be of a certain size regardless of the architecture and help in porting code to other platforms and languages which also share those types.
无论体系结构如何,这些都保证具有一定的大小,并有助于将代码移植到也共享这些类型的其他平台和语言。

