NSLog gives me warnings that cannot be fixed

I have the following line of code in a Mac OS X application:

NSLog(@"number of items: %ld", [urlArray count]); 

And I get a warning: "The format sets the type to" long ", but the argument is of the type" NSUInteger "(aka" unsigned int ")"

However, if I change my code to:

 NSLog(@"number of items: %u", [urlArray count]); 

I get a warning:

The format defines the type "unsigned int", but the argument is of the type "NSUInteger" (aka "unsigned long")

So, I change it to

 NSLog(@"number of items: %u", [urlArray count]); 

but I get a warning: The format sets the type to "unsigned long", but the argument is of type "NSUInteger" (aka "unsigned int")

How can I configure my NSLog so that it does not generate a warning? If I follow the Xcode guidelines, I’ll just go in for an endless cycle of changing the format specifier, but the warnings will never disappear.

+7
source share
3 answers

Yes, this is annoying. I believe this is 32/64 bit. The easiest way to do this is to just throw it on a long one:

 NSLog(@"number of items: %lu", (unsigned long)[urlArray count]); 
+14
source

portability guide for universal applications offers casting in this case.

 NSLog(@"number of items: %ld", (unsigned long)[urlArray count]); 
+6
source

Another option is mentioned here: NSInteger and NSUInteger in a 64bit / 32bit mixed environment

 NSLog(@"Number is %@", @(number)); 
+2
source

All Articles