bbjay
bbjay

Reputation: 1820

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 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?

Upvotes: 3

Views: 11170

Answers (2)

user1105951
user1105951

Reputation: 2287

Just want to add : I got this warning "suddenly" even when I did not change the code that made the warning for long time, and I did not understand why it's appear.

The reason : "Generic iOS Device". when selected device or simulator, the warnings disappear.

still, I add to the NSLog "(long)" to the variables. till then I only had "%ld", that turn the warning no matter what was selected (generic device, real device, simulator)

Upvotes: 0

Nat
Nat

Reputation: 12948

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);

Upvotes: 23

Related Questions