bbjay bbjay - 7 days ago 5x
Objective-C Question

Xcode warning: Format specifies type 'long' but the argument has type 'int _Nullable'

I get this warning for the following line of code:

NSLog(@"selected segment: %li", _segmentControl.selectedSegmentIndex);

The property
is of type

If I change the format to
i get the following warning:

Format specifies type 'int' but the argument has type 'long _Nullable'

Are there any new format specifiers for Nullable types or is this just a bug in Xcode 7?


You should type:

NSLog(@"selected segment: %li", (long)_segmentControl.selectedSegmentIndex);

Because NSInteger has a different length in 32 and 64bit architecture. Previously you didn't see the warning, because probably you was compiling only against 64 bit architecture.

I'd also advise to read Apple Article, as there are new specifiers in Xcode 7 (among others nullable and nonnull).

To answer your doubts from the comment, please refer to this Apple document, where they state the following:

Type Specifiers

Script action: Warns about potential problems; may generate false negatives.

Typically, in 32-bit code you use the %d specifier to format int values in functions such as printf, NSAssert, and NSLog, and in methods such as stringWithFormat:. But with NSInteger, which on 64-bit architectures is the same size as long, you need to use the %ld specifier. Unless you are building 32-bit like 64-bit, these specifiers generates compiler warnings in 32-bit mode. To avoid this problem, you can cast the values to long or unsigned long, as appropriate. For example:

NSInteger i = 34;
printf("%ld\n", (long)i);