NSUInteger should not be used in format strings?
Solution 1:
NSUInteger and NSInteger are different lengths on 32-bit (int) and 64-bit (long). In order for one format specifier to work for both architectures, you must use a long specifier and cast the value to long:
Type Format Specifier Cast
---- ---------------- ----
NSInteger %ld long
NSUInteger %lu unsigned long
So, for example, your code becomes:
[NSString stringWithFormat:@"Total Properties: %lu", (unsigned long)[inArray count]];
There is very little work to do, really, because Xcode's Fix-It feature will do this for you automatically.
Solution 2:
It is also possible to use the "z" and "t" modifiers for CPU-independent format strings, e.g.
NSInteger x = -1;
NSUInteger y = 99;
NSString *foo = [NSString stringWithFormat:@"NSInteger: %zd, NSUInteger: %tu", x, y];
Solution 3:
The underlying type of NSUInteger
changes based on the platform: it is a 32-bit unsigned integer on 32-bit platforms, and a 64-bit unsigned integer on 64-bit platforms.
In the Platform Dependencies section on of the String Programming Guide Apple suggests that you do the following:
To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.
For
NSUInteger
use format%lu
or%lx
, and cast the value tounsigned long
.
Hence your code needs to be changed as follows to avoid the warning:
[NSString stringWithFormat:@"Total Properties: %lu", (unsigned long)[inArray count]];