NSUInteger不应该在格式字符串中使用吗? [英] NSUInteger should not be used in format strings?

查看:136
本文介绍了NSUInteger不应该在格式字符串中使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的全部荣耀所在的代码:

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

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

哪个让我收到Xcode 5.1警告:

Which gets me an Xcode 5.1 warning:

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

好,所以我很困惑.该值实际上是32位int,我将其强制转换为32位int.那么,这个NSUInteger在抱怨什么(我认为是这个数字),为什么这个强制转换不能解决它?

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?

推荐答案

NSUInteger和NSInteger在32位(int)和64位(long)上具有不同的长度.为了使 one 格式说明符适用于两种体系结构,必须使用long说明符并将值强制转换为long:

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

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

例如,您的代码变为:

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

实际上,几乎没有什么工作要做,因为Xcode的Fix-It功能将自动为您完成此工作.

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

这篇关于NSUInteger不应该在格式字符串中使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆