我应该如何在Objective-C中声明一个long? NSInteger合适吗?

时间:2021-02-26 20:57:03

I see NSInteger is used quite often and the typedef for it on the iPhone is a long, so technically I could use it when I am expect int(64) values. But should I be more explicit and use something like int64_t or long directly? What would be the downside of just using long?

我看到NSInteger经常被使用,并且iPhone上的typedef很长,所以从技术上来说,当我期望int(64)值时,我可以使用它。但是我应该更明确地使用像int64_t或long这样的东西吗?使用多长时间的缺点是什么?

5 个解决方案

#1


15  

IIRC, long on the iPhone/ARM is 32 bits. If you want a guaranteed 64-bit integer, you should (indeed) use int64_t.

IIRC,长期以来iPhone / ARM是32位。如果你想要一个有保证的64位整数,你应该(确实)使用int64_t。

#2


2  

Integer Data Types Sizes

  • short - ILP32: 2 bytes; LP64: 2 bytes

    short - ILP32:2个字节; LP64:2个字节

  • int - ILP32: 4 bytes; LP64: 4 bytes

    int - ILP32:4个字节; LP64:4个字节

  • long - ILP32: 4 bytes; LP64: 8 bytes

    long - ILP32:4字节; LP64:8个字节

  • long long - ILP32: 8 bytes; LP64: 8 bytes

    long long - ILP32:8字节; LP64:8个字节

It may be useful to know that:

了解以下内容可能很有用:

The compiler defines the __LP64__ macro when compiling for the 64-bit runtime.

编译64位运行时时,编译器定义__LP64__宏。

NSInteger is a typedef of long so it will be 32-bits in a 32-bit environment and 64-bits in a 64-bit environment.

NSInteger是一个longde的typedef,因此它在32位环境中为32位,在64位环境中为64位。

When converting to 64-bit you can simply replace all your ints and longs to NSInteger and you should be good to go.

当转换为64位时,您可以简单地将所有的内容和长度替换为NSInteger,您应该很高兴。

Important: pay attention to the alignment of data, LP64 uses natural alignment for all Integer data types but ILP32 uses 4 bytes for all Integer data types with size equal to or greater than 4 bytes.

重要:注意数据的对齐,LP64对所有Integer数据类型使用自然对齐,但ILP32对所有大小等于或大于4个字节的Integer数据类型使用4个字节。

You can read more about 32 to 64 bit conversion in the Official 64-Bit Transition Guide for Cocoa Touch.

您可以在Cocoa Touch的官方64位转换指南中阅读有关32到64位转换的更多信息。

Answering you questions:

回答你的问题:

How should I declare a long in Objective-C? Is NSInteger appropriate?

我应该如何在Objective-C中声明一个long? NSInteger合适吗?

You can use either long or NSInteger but NSInteger is more idiomatic IMHO.

你可以使用long或NSInteger,但NSInteger是更惯用的恕我直言。

But should I be more explicit and use something like int64_t or long directly?

但是我应该更明确地使用像int64_t或long这样的东西吗?

If you expect consistent 64-bit sizes neither long nor NSInteger will do, you'll have to use int64_t (as Wevah said).

如果你期望64位大小都不长,NSInteger也不会这样做,你必须使用int64_t(正如Wevah所说)。

What would be the downside of just using long?

使用多长时间的缺点是什么?

It's not idiomatic and you may have problems if Apple rolls out a new architecture again.

这不是惯用的,如果Apple再次推出新的架构,你可能会遇到问题。

#3


0  

If you need a type of known specific size, use the type that has that known specific size: int64_t.

如果需要已知特定大小的类型,请使用具有已知特定大小的类型:int64_t。

If you need a generic integer type and the size is not important, go ahead and use int or NSInteger.

如果您需要通用整数类型且大小不重要,请继续使用int或NSInteger。

#4


0  

NSInteger's length depends on whether you are compiling for 32 bit or 64 bit. It's defined as long for 64 bit and iPhone and int for 32 bit.

NSInteger的长度取决于您是编译32位还是64位。它被定义为64位和iPhone的长度和32位的int。

So on iPhone the length of NSInteger is the same as the length of a long, which is compiler dependent. Most compilers make long the same length as the native word. i.e. 32 bit for 32 bit architectures and 64 bit for 64 bit architectures.

所以在iPhone上,NSInteger的长度与long的长度相同,这取决于编译器。大多数编译器的长度与原始单词的长度相同。即32位架构的32位和64位架构的64位。

Given the uncertainty over the width of NSInteger, I use it only for types of variables to be used in the Cocoa API when NSInteger is specified. If I need a fixed width type, I go for the ones defined in stdint.h. If I don't care about the width I use the C built in types

鉴于NSInteger宽度的不确定性,我只将它用于指定NSInteger时在Cocoa API中使用的变量类型。如果我需要一个固定宽度类型,我会选择stdint.h中定义的类型。如果我不关心宽度,我使用C内置类型

#5


0  

If you want to declare something as long, declare it as long. Be aware that long can be 32 or 64 bit, depending on the compiler.

如果你想要声明一些东西,请将其声明为long。请注意,long可以是32位或64位,具体取决于编译器。

If you want to declare something to be as efficient as possible, and big enough to count items, use NSInteger or NSUInteger. Note that both can be 32 or 64 bits, and can be actually different types (int or long), depending on the compiler. Which protects you from mixing up types in some cases.

如果要声明某些内容尽可能高效,并且足够大以计算项目,请使用NSInteger或NSUInteger。请注意,两者都可以是32位或64位,实际上可以是不同的类型(int或long),具体取决于编译器。这可以保护您在某些情况下不会混淆类型。

If you want 32 or 64 bit, and nothing else, use int32_t, uint32_t, int64_t, uint64_t. Be aware that either type can be unnecessarily inefficient on some compiler.

如果你想要32或64位,没有别的,请使用int32_t,uint32_t,int64_t,uint64_t。请注意,在某些编译器上,任何一种类型都可能不必要地低效。

#1


15  

IIRC, long on the iPhone/ARM is 32 bits. If you want a guaranteed 64-bit integer, you should (indeed) use int64_t.

IIRC,长期以来iPhone / ARM是32位。如果你想要一个有保证的64位整数,你应该(确实)使用int64_t。

#2


2  

Integer Data Types Sizes

  • short - ILP32: 2 bytes; LP64: 2 bytes

    short - ILP32:2个字节; LP64:2个字节

  • int - ILP32: 4 bytes; LP64: 4 bytes

    int - ILP32:4个字节; LP64:4个字节

  • long - ILP32: 4 bytes; LP64: 8 bytes

    long - ILP32:4字节; LP64:8个字节

  • long long - ILP32: 8 bytes; LP64: 8 bytes

    long long - ILP32:8字节; LP64:8个字节

It may be useful to know that:

了解以下内容可能很有用:

The compiler defines the __LP64__ macro when compiling for the 64-bit runtime.

编译64位运行时时,编译器定义__LP64__宏。

NSInteger is a typedef of long so it will be 32-bits in a 32-bit environment and 64-bits in a 64-bit environment.

NSInteger是一个longde的typedef,因此它在32位环境中为32位,在64位环境中为64位。

When converting to 64-bit you can simply replace all your ints and longs to NSInteger and you should be good to go.

当转换为64位时,您可以简单地将所有的内容和长度替换为NSInteger,您应该很高兴。

Important: pay attention to the alignment of data, LP64 uses natural alignment for all Integer data types but ILP32 uses 4 bytes for all Integer data types with size equal to or greater than 4 bytes.

重要:注意数据的对齐,LP64对所有Integer数据类型使用自然对齐,但ILP32对所有大小等于或大于4个字节的Integer数据类型使用4个字节。

You can read more about 32 to 64 bit conversion in the Official 64-Bit Transition Guide for Cocoa Touch.

您可以在Cocoa Touch的官方64位转换指南中阅读有关32到64位转换的更多信息。

Answering you questions:

回答你的问题:

How should I declare a long in Objective-C? Is NSInteger appropriate?

我应该如何在Objective-C中声明一个long? NSInteger合适吗?

You can use either long or NSInteger but NSInteger is more idiomatic IMHO.

你可以使用long或NSInteger,但NSInteger是更惯用的恕我直言。

But should I be more explicit and use something like int64_t or long directly?

但是我应该更明确地使用像int64_t或long这样的东西吗?

If you expect consistent 64-bit sizes neither long nor NSInteger will do, you'll have to use int64_t (as Wevah said).

如果你期望64位大小都不长,NSInteger也不会这样做,你必须使用int64_t(正如Wevah所说)。

What would be the downside of just using long?

使用多长时间的缺点是什么?

It's not idiomatic and you may have problems if Apple rolls out a new architecture again.

这不是惯用的,如果Apple再次推出新的架构,你可能会遇到问题。

#3


0  

If you need a type of known specific size, use the type that has that known specific size: int64_t.

如果需要已知特定大小的类型,请使用具有已知特定大小的类型:int64_t。

If you need a generic integer type and the size is not important, go ahead and use int or NSInteger.

如果您需要通用整数类型且大小不重要,请继续使用int或NSInteger。

#4


0  

NSInteger's length depends on whether you are compiling for 32 bit or 64 bit. It's defined as long for 64 bit and iPhone and int for 32 bit.

NSInteger的长度取决于您是编译32位还是64位。它被定义为64位和iPhone的长度和32位的int。

So on iPhone the length of NSInteger is the same as the length of a long, which is compiler dependent. Most compilers make long the same length as the native word. i.e. 32 bit for 32 bit architectures and 64 bit for 64 bit architectures.

所以在iPhone上,NSInteger的长度与long的长度相同,这取决于编译器。大多数编译器的长度与原始单词的长度相同。即32位架构的32位和64位架构的64位。

Given the uncertainty over the width of NSInteger, I use it only for types of variables to be used in the Cocoa API when NSInteger is specified. If I need a fixed width type, I go for the ones defined in stdint.h. If I don't care about the width I use the C built in types

鉴于NSInteger宽度的不确定性,我只将它用于指定NSInteger时在Cocoa API中使用的变量类型。如果我需要一个固定宽度类型,我会选择stdint.h中定义的类型。如果我不关心宽度,我使用C内置类型

#5


0  

If you want to declare something as long, declare it as long. Be aware that long can be 32 or 64 bit, depending on the compiler.

如果你想要声明一些东西,请将其声明为long。请注意,long可以是32位或64位,具体取决于编译器。

If you want to declare something to be as efficient as possible, and big enough to count items, use NSInteger or NSUInteger. Note that both can be 32 or 64 bits, and can be actually different types (int or long), depending on the compiler. Which protects you from mixing up types in some cases.

如果要声明某些内容尽可能高效,并且足够大以计算项目,请使用NSInteger或NSUInteger。请注意,两者都可以是32位或64位,实际上可以是不同的类型(int或long),具体取决于编译器。这可以保护您在某些情况下不会混淆类型。

If you want 32 or 64 bit, and nothing else, use int32_t, uint32_t, int64_t, uint64_t. Be aware that either type can be unnecessarily inefficient on some compiler.

如果你想要32或64位,没有别的,请使用int32_t,uint32_t,int64_t,uint64_t。请注意,在某些编译器上,任何一种类型都可能不必要地低效。