C语言 NSUInteger 与 NSInteger、int 与 unsigned 以及类似情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2557209/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:01:31  来源:igfitidea点击:

NSUInteger vs NSInteger, int vs unsigned, and similar cases

cobjective-ccocoatypes

提问by Jesse Armand

Anyone have the expertise to explain when to use NSUInteger and when to use NSInteger?

任何人都有专业知识来解释何时使用 NSUInteger 以及何时使用 NSInteger?

I had seen Cocoa methods returning NSInteger even in cases where the returned value will always be unsigned.

我见过 Cocoa 方法返回 NSInteger,即使在返回值总是无符号的情况下也是如此。

What is the fundamental reason? Is NSInteger or int strictly limited to if we want to represent negative value?

根本原因是什么?如果我们想表示负值,NSInteger 或 int 是否严格限于?

From NSObjCRuntime.h:

来自 NSObjCRuntime.h:

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

回答by Erik Abele

You should also be aware of integer conversion rules when dealing with NSUInteger vs. NSInteger:

在处理 NSUInteger 与 NSInteger 时,您还应该了解整数转换规则:

The following fragment for example returns 0 (false) although you'd expect it to print 1 (true):

例如,以下片段返回 0 (false),尽管您希望它打印 1 (true):

NSInteger si = -1;
NSUInteger ui = 1;
printf("%d\n", si < ui);

The reason is that the [si] variable is being implicitly converted to an unsigned int!

原因是 [si] 变量被隐式转换为 unsigned int!

See CERT's Secure Coding sitefor an in-depth discussion around these 'issues' and how to solve them.

有关这些“问题”以及如何解决这些问题的深入讨论,请参阅CERT 的安全编码站点

回答by Krishna Kishore

By default, an integer is assumed to be signed. In other words the compiler assumes that an integer variable will be called upon to store either a negative or positive number. This limits the extent that the range can reach in either direction. For example, a 32-bit int has a range of 4,294,967,295. In practice, because the value could be positive or negative the range is actually ?2,147,483,648 to +2,147,483,647. If we know that a variable will never be called upon to store a negative value, we can declare it as unsigned, thereby extending the (positive) range to 0 to +4,294,967,295. So i would say, its okay to use NSInteger when you know that you have a restricted output range. I personally use NSUInteger if I needed to return really big positive only numbers

默认情况下,假定整数是有符号的。换句话说,编译器假定将调用整数变量来存储负数或正数。这限制了范围可以在任一方向达到的范围。例如,32 位 int 的范围为 4,294,967,295。实际上,由于该值可能为正或为负,因此范围实际上是 ?2,147,483,648 到 +2,147,483,647。如果我们知道永远不会调用一个变量来存储负值,我们可以将其声明为无符号,从而将(正)范围扩展到 0 到 +4,294,967,295。所以我想说,当你知道你有一个受限的输出范围时,可以使用 NSInteger。如果我需要返回非常大的正数,我个人会使用 NSUInteger

回答by Carl Norum

If your method has a suitably restricted output range, you might as well use NSIntegersince it's easier to type. As you say, if you need to return negative numbers, NSIntegeris the only game in town; I'd only use NSUIntegerif I needed to return reallybig numbers for some reason.

如果您的方法具有适当限制的输出范围,您不妨使用NSInteger它,因为它更易于输入。如您所说,如果您需要返回负数,NSInteger则是镇上唯一的游戏;我只会NSUInteger在出于某种原因需要返回非常大的数字时使用。

回答by AaronLS

I don't know about cocoa specifically, but usually the only disadvantage of signed integers is they usually have half the maximum value as a unsigned int. Such as about 2 billion instead of 4 billion for 32bit systems. Generally this is not a big difference, since if you are dealing with values close to 2 billion, you probably would be just as worried about a 4 billion max in the same case, since it is still putting pretty close to an overflow with just a multiplication of 2 or 3.

我不特别了解可可,但通常有符号整数的唯一缺点是它们的最大值通常是无符号整数的一半。例如大约 20 亿而不是 32 位系统的 40 亿。一般来说,这不是一个很大的区别,因为如果你处理接近 20 亿的值,你可能会同样担心在同一情况下 40 亿的最大值,因为它仍然非常接近溢出,只有一个2 或 3 的乘法。

Signed integers are usually preferred because the additional flexibility and fact that a signed integer can be used in almost all the scenarios an unsigned integer can plus all the additional scenarios it cannot where a negative is needed.

有符号整数通常是首选,因为有符号整数可以用于几乎所有场景中的额外灵活性和事实,无符号整数可以加上所有其他场景,它不能在需要负数的情况下使用。

Unsigned might be preferred if you want to enforce positive only values.

如果您只想强制执行正值,则可能首选无符号。