xcode 在 iOS 和 OSX 上使用 unsigned int 和 unsigned long 编译 NSLog 没有警告

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

Compile NSLog with unsigned int and unsigned long on iOS and OSX without a warning

iosxcodemacos

提问by hanno

On iOS NSUIntegeris a unsigned int, on OSX it is a unsigned long. How can I make a print statement like

在 iOS 上NSUInteger是一个unsigned int,在 OSX 上它是一个unsigned long. 我怎样才能做出像这样的打印声明

 NSLog(@"Array has %d elements.",[array count]);

compile on both platforms without a warning? I can of course use an #ifdef #else #endifconstruct but that will add 4 lines of code. I could also cast the return value to unsigned int. Is there a shorter solution?

在没有警告的情况下在两个平台上编译?我当然可以使用一个#ifdef #else #endif构造,但这会增加 4 行代码。我也可以将返回值转换为 unsigned int。有更短的解决方案吗?

回答by danh

How about a cast up to the larger of the two?

铸造到两者中较大的一个怎么样?

NSLog(@"Array has %ld elements.",(unsigned long)[array count]);

No warning in iOS, and I think it's a no-op in OSX.

在 iOS 中没有警告,我认为它在 OSX 中是空的。

回答by Nguy?n Kh?c Hoàng

How about a cast up to the larger of the two?

铸造到两者中较大的一个怎么样?

NSLog(@"Array has %ld elements.",(unsigned long)[array count]);
No warning in iOS, and I think it's a no-op in OSX.