Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSInteger and NSUInteger in a mixed 64bit / 32bit environment

I have a fair amount of string format specifiers in NSLog / NSAssert etc. calls which use %d and %u with NSInteger (= int on 32bit) and NSUInteger (= unsigned int on 32bit) types respectively.

When converting the app to 64bit, this gives warnings (of course), as %ld %lu is expected for what now became a long and unsigned long type.

Simply converting the format specifiers will of course introduce the reverse warnings in the 32bit build.
So the only solution I see to become warning free is using the 64bit specifiers, and casting to the 64bit value types everywhere a warning is given in the 32bit build.

But I was wondering if perhaps there are format specifiers specifically for the NSInteger and NSUInteger type which would work on both architectures without casting?

like image 558
Pieter Avatar asked Dec 03 '13 15:12

Pieter


2 Answers

I think the safest way is to box them into NSNumber instances.

NSLog(@"Number is %@", @(number)); // use the highest level of abstraction 

This boxing doesn't usually have to create a new object thanks to tagged pointer magic.

If you really don't want to use NSNumber, you can cast primitive types manually, as others suggested:

NSLog(@"Number is %ld", (long)number); // works the same on 32-bit and 64-bit 
like image 188
ilya n. Avatar answered Sep 21 '22 05:09

ilya n.


You can also use %zd (NSInteger) and %tu (NSUInteger) when logging to the console.

NSInteger integer = 1; NSLog(@"first number: %zd", integer);  NSUInteger uinteger = 1; NSLog(@"second number: %tu", uinteger); 

Also to be found here.

like image 36
d0m1n1k Avatar answered Sep 25 '22 05:09

d0m1n1k