使用NSInteger而不是int的好处?

时间:2021-06-06 21:02:27

I am trying to comprehend how development is affected when developing for both 32-bit and 64-bit architectures. From what I have researched thus far, I understand an int is always 4 bytes regardless of the architecture of the device running the app. But an NSInteger is 4 bytes on a 32-bit device and 8 bytes on a 64-bit device. I get the impression NSInteger is "safer" and recommended but I'm not sure what the reasoning is for that.

我试图理解在开发32位和64位架构时如何影响开发。根据我迄今为止所研究的内容,我理解无论运行应用程序的设备的架构如何,int总是4个字节。但NSInteger在32位设备上是4个字节,在64位设备上是8个字节。我得到的印象NSInteger“更安全”并推荐但我不确定是什么原因。

My question is, if you know the possible value you're using is never going to be large (maybe you're using it to index into an array of 200 items or store the count of objects in an array), why define it as an NSInteger? That's just going to take up 8 bytes when you won't use it all. Is it better to define it as an int in those cases? If so, in what case would you want to use an NSInteger (as opposed to int or long etc)? Obviously if you needed to utilize larger numbers, you could with the 64-bit architecture. But if you needed it to also work on 32-bit devices, would you not use long long because it's 8 bytes on 32-bit devices as well? I don't see why one would use NSInteger, at least when creating an app that runs on both architectures.

我的问题是,如果你知道你正在使用的可能值永远不会很大(也许你用它来索引一个包含200个项目的数组或者存储数组中对象的数量),为什么要将它定义为一个NSInteger?当你不使用它时,这只会占用8个字节。在这些情况下将它定义为int更好吗?如果是这样,在什么情况下你想使用NSInteger(而不是int或long等)?显然,如果你需要使用更大的数字,你可以使用64位架构。但是如果你需要它也可以在32位设备上工作,你会不会长时间使用它,因为它在32位设备上也是8字节?我不明白为什么会使用NSInteger,至少在创建在两种架构上运行的应用程序时都是如此。

Also I cannot think of a method which takes in or returns a primitive type - int, and instead utilizes NSInteger, and am wondering if there is more to it than just the size of the values. For example, (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section. I'd like to understand why this is the case. Assuming it's possible to have a table with 2,147,483,647 rows, what would occur on a 32-bit device when you add one more - does it wrap around to a -2,147,483,647? And on a 64-bit device it would be 2,147,483,648. (Why return a signed value? I'd think it should be unsigned since you can't have a negative number of rows.)

另外,我想不出一个接受或返回基本类型的方法 - int,而是使用NSInteger,并且想知道它是否还有更多,而不仅仅是值的大小。例如,(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分。我想明白为什么会这样。假设有一个2,147,483,647行的表,当你再添加一个32位设备时会发生什么 - 它会回绕到-2,147,483,647吗?在64位设备上,它将是2,147,483,648。 (为什么返回有符号值?我认为它应该是无符号的,因为你不能有负数行。)

Ultimately, I'd like to obtain a better understanding of actual use of these number data types, perhaps some code examples would be great!

最后,我想更好地理解这些数字数据类型的实际使用,也许一些代码示例会很棒!

4 个解决方案

#1


2  

I personally think that, 64-bit is actually the reason for existence 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。

NSInteger/NSUInteger are defined as *dynamic typedef*s to one of these types, and they are defined like this:

NSInteger / NSUInteger被定义为* dynamic typedef * s到这些类型之一,它们的定义如下:

#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类型。

I suggest you to throughly read this link. CocoaDev has some more info.

我建议你仔细阅读这个链接。 CocoaDev有更多信息。

For proper format specifier you should use for each of these types, see the String Programming Guide's section on Platform Dependencies

对于适用于这些类型的格式说明符,请参阅“字符串编程指南”中有关平台依赖性的部分

#2


2  

I remember when attending iOS developer conference. you have to take a look on the data-type in iOS7. for example, you use NSInteger in 64-bit device and save it on iCloud. then you want to sync to lower device (say iPad 2nd gen), your app will not behave the same, because it recognizes NSInteger in 4 bytes not 8 bytes, then your calculation would be wrong.

我记得在参加iOS开发者大会时。你必须看看iOS7中的数据类型。例如,您在64位设备中使用NSInteger并将其保存在iCloud上。然后你想同步到较低的设备(比如iPad第二代),你的应用程序将不会表现相同,因为它识别4字节的NSInteger而不是8字节,那么你的计算将是错误的。

But so far, I use NSInteger because mostly my app doesn't use iCloud or doesn't sync. and to avoid compiler warning.

但到目前为止,我使用的是NSInteger,因为我的应用程序主要不使用iCloud或不同步。并避免编译器警告。

#3


1  

Apple uses int because for a loop control variable (which is only used to control the loop iterations) int datatype is fine, both in datatype size and in the values it can hold for your loop. No need for platform dependent datatype here. For a loop control variable even a 16-bit int will do most of the time.

Apple使用int,因为对于循环控制变量(仅用于控制循环迭代),int数据类型很好,无论是数据类型大小还是它可以为循环保存的值。这里不需要平台相关的数据类型。对于循环控制变量,即使是16位的int也会在大部分时间内完成。

Apple uses NSInteger for a function return value or for a function argument because in this case datatype [size] matters, because what you are doing with a function is communicating/passing data with other programs or with other pieces of code.

Apple使用NSInteger作为函数返回值或函数参数,因为在这种情况下,数据类型[size]很重要,因为您正在使用函数正在与其他程序或其他代码进行通信/传递数据。

Apple uses NSInteger (or NSUInteger) when passing a value as an argument to a function or returning a value from a function.

Apple将值作为参数传递给函数或从函数返回值时使用NSInteger(或NSUInteger)。

The only thing I would use NSInteger for is passing values to and from an API that specifies it. Other than that it has no advantage over an int or a long. At least with an int or a long you know what format specifiers to use in a printf or similar statement.

我唯一会使用NSInteger的是将值传递给指定它的API。除此之外,它没有优于int或long。至少使用int或long你知道在printf或类似语句中使用什么格式说明符。

#4


1  

As a continue to Irfan's answer: sizeof(NSInteger) equals a processor word's size. It is much more simple and faster for processor to operate with words

继续Irfan的回答:sizeof(NSInteger)等于处理器字的大小。处理器使用单词操作更简单,更快捷

#1


2  

I personally think that, 64-bit is actually the reason for existence 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。

NSInteger/NSUInteger are defined as *dynamic typedef*s to one of these types, and they are defined like this:

NSInteger / NSUInteger被定义为* dynamic typedef * s到这些类型之一,它们的定义如下:

#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类型。

I suggest you to throughly read this link. CocoaDev has some more info.

我建议你仔细阅读这个链接。 CocoaDev有更多信息。

For proper format specifier you should use for each of these types, see the String Programming Guide's section on Platform Dependencies

对于适用于这些类型的格式说明符,请参阅“字符串编程指南”中有关平台依赖性的部分

#2


2  

I remember when attending iOS developer conference. you have to take a look on the data-type in iOS7. for example, you use NSInteger in 64-bit device and save it on iCloud. then you want to sync to lower device (say iPad 2nd gen), your app will not behave the same, because it recognizes NSInteger in 4 bytes not 8 bytes, then your calculation would be wrong.

我记得在参加iOS开发者大会时。你必须看看iOS7中的数据类型。例如,您在64位设备中使用NSInteger并将其保存在iCloud上。然后你想同步到较低的设备(比如iPad第二代),你的应用程序将不会表现相同,因为它识别4字节的NSInteger而不是8字节,那么你的计算将是错误的。

But so far, I use NSInteger because mostly my app doesn't use iCloud or doesn't sync. and to avoid compiler warning.

但到目前为止,我使用的是NSInteger,因为我的应用程序主要不使用iCloud或不同步。并避免编译器警告。

#3


1  

Apple uses int because for a loop control variable (which is only used to control the loop iterations) int datatype is fine, both in datatype size and in the values it can hold for your loop. No need for platform dependent datatype here. For a loop control variable even a 16-bit int will do most of the time.

Apple使用int,因为对于循环控制变量(仅用于控制循环迭代),int数据类型很好,无论是数据类型大小还是它可以为循环保存的值。这里不需要平台相关的数据类型。对于循环控制变量,即使是16位的int也会在大部分时间内完成。

Apple uses NSInteger for a function return value or for a function argument because in this case datatype [size] matters, because what you are doing with a function is communicating/passing data with other programs or with other pieces of code.

Apple使用NSInteger作为函数返回值或函数参数,因为在这种情况下,数据类型[size]很重要,因为您正在使用函数正在与其他程序或其他代码进行通信/传递数据。

Apple uses NSInteger (or NSUInteger) when passing a value as an argument to a function or returning a value from a function.

Apple将值作为参数传递给函数或从函数返回值时使用NSInteger(或NSUInteger)。

The only thing I would use NSInteger for is passing values to and from an API that specifies it. Other than that it has no advantage over an int or a long. At least with an int or a long you know what format specifiers to use in a printf or similar statement.

我唯一会使用NSInteger的是将值传递给指定它的API。除此之外,它没有优于int或long。至少使用int或long你知道在printf或类似语句中使用什么格式说明符。

#4


1  

As a continue to Irfan's answer: sizeof(NSInteger) equals a processor word's size. It is much more simple and faster for processor to operate with words

继续Irfan的回答:sizeof(NSInteger)等于处理器字的大小。处理器使用单词操作更简单,更快捷