Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

macos

ios

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

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

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

like image 251
hanno Avatar asked Jan 16 '13 01:01

hanno


1 Answers

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.

like image 160
danh Avatar answered Nov 08 '22 22:11

danh