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

10,006

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);
Share:
10,006
bbjay
Author by

bbjay

Updated on June 08, 2022

Comments

  • bbjay
    bbjay about 2 years

    I get this warning for the following line of code:

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

    The property selectedSegmentIndex is of type NSInteger.

    If I change the format to %i 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?