在iOS和OSX上使用unsigned int和unsigned long编译NSLog而不发出警告

han*_*nno 8 macos xcode ios

在iOS上NSUInteger是一个unsigned int,在OSX上它是一个unsigned long.我怎么能做一个类似的打印声明

 NSLog(@"Array has %d elements.",[array count]);
Run Code Online (Sandbox Code Playgroud)

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

dan*_*anh 17

如何投射到两者中的较大者?

NSLog(@"Array has %ld elements.",(unsigned long)[array count]);
Run Code Online (Sandbox Code Playgroud)

在iOS中没有警告,我认为它在OSX中是无操作的.

  • 最好的方法是`NSLog(@"%lu",(unsigned long)array.count);`用于`NSUInteger`和`NSLog(@"%ld",(long)button.tag);`用于`NSInteger `:32位或64位无警告. (3认同)