objective-c NSUInteger 不应该用在格式字符串中吗?

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

NSUInteger should not be used in format strings?

objective-c

提问by Maury Markowitz

Here's my code in all it's glory:

这是我所有的荣耀代码:

[NSString stringWithFormat:@"Total Properties: %d", (int)[inArray count]];

Which gets me an Xcode 5.1 warning:

这让我收到了 Xcode 5.1 警告:

Values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead

Ok so I'm confused. The value really is a 32-bit int, and I cast it to a 32-bit int. So what is this NSUInteger it's complaining about (the count I assume) and why doesn't this cast fix it?

好吧,我很困惑。该值确实是一个 32 位 int,我将其转换为 32 位 int。那么它抱怨的这个 NSUInteger 是什么(我假设的计数),为什么这个演员不修复它?

回答by matt

NSUInteger and NSInteger are different lengths on 32-bit (int) and 64-bit (long). In order for oneformat specifier to work for botharchitectures, you must use a long specifier and cast the value to long:

NSUInteger 和 NSInteger 在 32 位 (int) 和 64 位 (long) 上的长度不同。为了让一个格式说明符同时适用于两种架构,您必须使用长说明符并将值转换为 long:

Type    Format Specifier    Cast
----    ----------------    ----
NSInteger    %ld            long
NSUInteger   %lu            unsigned long

So, for example, your code becomes:

因此,例如,您的代码变为:

[NSString stringWithFormat:@"Total Properties: %lu", (unsigned long)[inArray count]];

There is very little work to do, really, because Xcode's Fix-It feature will do this for you automatically.

实际上,要做的工作很少,因为 Xcode 的 Fix-It 功能会自动为您完成这项工作。

回答by Nick Lockwood

It is also possible to use the "z" and "t" modifiers for CPU-independent format strings, e.g.

也可以将“z”和“t”修饰符用于独立于 CPU 的格式字符串,例如

NSInteger x = -1;
NSUInteger y = 99;
NSString *foo = [NSString stringWithFormat:@"NSInteger: %zd, NSUInteger: %tu", x, y];

回答by dasblinkenlight

The underlying type of NSUIntegerchanges based on the platform: it is a 32-bit unsigned integer on 32-bit platforms, and a 64-bit unsigned integer on 64-bit platforms.

NSUInteger基于平台变化的底层类型:在32位平台上是32位无符号整数,在64位平台上是64位无符号整数。

In the Platform Dependenciessection on of the String Programming GuideApple suggests that you do the following:

String Programming GuidePlatform Dependencies部分,Apple 建议您执行以下操作:

To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.

For NSUIntegeruse format %luor %lx, and cast the value to unsigned long.

为避免需要根据平台使用不同的 printf 样式类型说明符,您可以使用表 3 中显示的说明符。请注意,在某些情况下,您可能需要强制转换值。

对于NSUInteger使用格式%luor %lx,并将值转换为unsigned long.

Hence your code needs to be changed as follows to avoid the warning:

因此,您的代码需要更改如下以避免警告:

[NSString stringWithFormat:@"Total Properties: %lu", (unsigned long)[inArray count]];

回答by CyberMoai

You could also try using NSNumber methods:

您也可以尝试使用 NSNumber 方法:

[NSString stringWithFormat:@"Total Properties: %@", [[NSNumber numberWithUnsignedInteger:[inArray count]] stringValue]];