简体   繁体   English

为什么在32位iOS上未将NSInteger定义为很长?

[英]Why isn't NSInteger defined as long on 32-bit iOS?

NSInteger is defined this way: NSInteger是这样定义的:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

This results in NSInteger being defined as int on 32-bit iOS even though int and long are the same anyway (both 4 bytes). 这导致NSInteger在32位iOS上被定义为int,即使int和long仍然相同(均为4个字节)。 Format strings like the following generate a warning with this definition: 如下所示的格式字符串会使用此定义生成警告:

NSInteger x = 4;
[NSString stringWithFormat: @"%ld", x];

// Warning: Values of type 'NSInteger' should not be used as format arguments;
// add an explicit cast to 'long' instead.

So does somebody know why NSInteger isn't always defined as long? 那么有人知道为什么NSInteger并不总是定义得那么长吗?

Historical reasons, where previous releases of APIs used int -- then were migrated to use typedef NSInteger ca. 出于历史原因,以前的API版本使用int ,然后又迁移到使用typedef NSInteger the 64-bit transition of OS X. OS X的64位过渡。

I suppose they could have changed for iOS, but that would have impacted a lot of existing and to-be-developed code if they were different on OS X and iOS. 我想它们可能会针对iOS进行更改,但是如果它们在OS X和iOS上有所不同,则会影响许多现有和待开发的代码。

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

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