With the 64 bit version of iOS we can't use %d
and %u
anymore to format NSInteger
and NSUInteger
. Because for 64 bit those are typedef'd to long
and unsigned long
instead of int
and unsigned int
.
So Xcode will throw warnings if you try to format NSInteger with %d. Xcode is nice to us and offers an replacement for those two cases, which consists of a l-prefixed format specifier and a typecast to long. Then our code basically looks like this:
NSLog(@"%ld", (long)i);
NSLog(@"%lu", (unsigned long)u);
Which, if you ask me, is a pain in the eye.
A couple of days ago someone at Twitter mentioned the format specifiers %zd
to format signed variables and %tu
to format unsigned variables on 32 and 64 bit plattforms.
NSLog(@"%zd", i);
NSLog(@"%tu", u);
Which seems to work. And which I like more than typecasting.
But I honestly have no idea why those work. Right now both are basically magic values for me.
I did a bit of research and figured out that the z
prefix means that the following format specifier has the same size as size_t
. But I have absolutely no idea what the prefix t
means. So I have two questions:
What exactly do %zd
and %tu
mean?
And is it safe to use %zd
and %tu
instead of Apples suggestion to typecast to long?
I am aware of similar questions and Apples 64-Bit Transition guides, which all recommend the %lu (unsigned long)
approach. I am asking for an alternative to type casting.
%i
is exactly equivalent to%d
and does not help here. Unfortunately, there is no format specifier for NS(U)Integer. – Illfounded