NSU整数转int
我编写了一个使用 myarray 的方法,在同一个类中定义。当我使用 count 时,它总是返回 0。 当我使用:
printf("%d", [myarray count]);
编译器说:
Format '%d' expetcs type 'int', but argument 2 has type 'NSUInteger'
为什么?
I wrote a method tha uses myarray, defined in the same class. When I use count it always returns 0.
When I use:
printf("%d", [myarray count]);
compiler says:
Format '%d' expetcs type 'int', but argument 2 has type 'NSUInteger'
why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
%lu
而不是%d
。编译器根据您传递给 printf 的参数检查格式字符串,发现您传递的是无符号但将其打印为有符号整数,并发出警告。该警告表明,对于大于或等于 2^31 的数字,当数据类型暗示不同的语义(即大的正整数)时,printf
将输出大的负数。已编辑以回应 Josh Caswell 和 thepepp 的评论
You should use
%lu
instead of%d
. The compiler checks your format string against the parameters that you are passing toprintf
, sees that you are passing an unsigned but print it as a signed integer, and issues a warning. The warning indicates that for numbers greater than or equal to 2^31printf
would output a large negative number, when the data type implies a different semantic, namely, a large positive integer.EDITED in response to comments by Josh Caswell and thepepp