简体   繁体   English

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

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

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. 我看到NSInteger经常被使用,并且iPhone上的typedef很长,所以从技术上来说,当我期望int(64)值时,我可以使用它。 But should I be more explicit and use something like int64_t or long directly? 但是我应该更明确地使用像int64_t或long这样的东西吗? What would be the downside of just using long? 使用多长时间的缺点是什么?

IIRC, long on the iPhone/ARM is 32 bits. IIRC, long iPhone / ARM是32位。 If you want a guaranteed 64-bit integer, you should (indeed) use int64_t . 如果你想要一个有保证的64位整数,你应该(确实)使用int64_t

Integer Data Types Sizes 整数数据类型大小

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

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

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

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

It may be useful to know that: 了解以下内容可能很有用:

The compiler defines the __LP64__ macro when compiling for the 64-bit runtime. 编译64位运行__LP64__ ,编译器定义__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是一个typedeflong所以这将是在32位的环境32位和64位中的64位的环境。

When converting to 64-bit you can simply replace all your int s and long s to NSInteger and you should be good to go. 转换为64位时,您可以简单地将所有intlong s替换为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? 我应该如何在Objective-C中声明一个long? Is NSInteger appropriate? NSInteger合适吗?

You can use either long or NSInteger but NSInteger is more idiomatic IMHO. 你可以使用longNSIntegerNSInteger是更惯用的恕我直言。

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位大小都不longNSInteger也不会这样做,你必须使用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再次推出新的架构,你可能会遇到问题。

If you want to declare something as long, declare it as long. 如果你想声明一些东西,请将其声明为long。 Be aware that long can be 32 or 64 bit, depending on the compiler. 请注意,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. 如果要声明某些内容尽可能高效,并且足够大以计算项目,请使用NSInteger或NSUInteger。 Note that both can be 32 or 64 bits, and can be actually different types (int or long), depending on the compiler. 请注意,两者都可以是32位或64位,实际上可以是不同的类型(int或long),具体取决于编译器。 Which protects you from mixing up types in some cases. 在某些情况下,它可以防止混淆类型。

If you want 32 or 64 bit, and nothing else, use int32_t, uint32_t, int64_t, uint64_t. 如果你想要32或64位,没有别的,请使用int32_t,uint32_t,int64_t,uint64_t。 Be aware that either type can be unnecessarily inefficient on some compiler. 请注意,在某些编译器上,任何一种类型都可能不必要地低效。

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 . 如果您需要通用整数类型且大小不重要,请继续使用intNSInteger

NSInteger's length depends on whether you are compiling for 32 bit or 64 bit. NSInteger的长度取决于您是编译32位还是64位。 It's defined as long for 64 bit and iPhone and int for 32 bit. 它定义为64位和iPhone的长度和32位的int。

So on iPhone the length of NSInteger is the same as the length of a long, which is compiler dependent. 所以在iPhone上,NSInteger的长度与long的长度相同,这取决于编译器。 Most compilers make long the same length as the native word. 大多数编译器的长度与原生单词的长度相同。 ie 32 bit for 32 bit architectures and 64 bit for 64 bit architectures. 即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. 鉴于NSInteger宽度的不确定性,我只将它用于指定NSInteger时在Cocoa API中使用的变量类型。 If I need a fixed width type, I go for the ones defined in stdint.h . 如果我需要固定宽度类型,我会选择stdint.h定义的类型。 If I don't care about the width I use the C built in types 如果我不关心宽度,我使用C内置类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM