NSU整数转int

发布于 2024-12-26 10:57:54 字数 243 浏览 4 评论 0原文

我编写了一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2025-01-02 10:57:54

您应该使用 %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 to printf, 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^31 printf 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文