Xcode 警告:格式指定类型 'long' 但参数的类型为 'int _Nullable'
Xcode warning: Format specifies type 'long' but the argument has type 'int _Nullable'
我收到以下代码行的警告:
NSLog(@"selected segment: %li", _segmentControl.selectedSegmentIndex);
属性 selectedSegmentIndex
的类型是 NSInteger
。
如果我将格式更改为 %i
,我会收到以下警告:
Format specifies type 'int' but the argument has type 'long _Nullable'
可空类型是否有任何新的格式说明符,或者这只是 Xcode 7 中的一个错误?
你应该输入:
NSLog(@"selected segment: %li", (long)_segmentControl.selectedSegmentIndex);
因为 NSInteger 在 32 位和 64 位架构中的长度不同。以前您没有看到警告,因为您可能只针对 64 位架构进行编译。
我还建议阅读 Apple Article,因为 Xcode 7 中有新的说明符(以及 nullable
和 nonnull
)。
要回答您对评论的疑问,请参阅此 Apple document,其中说明如下:
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);
只想补充:
我收到这个警告 "suddenly" 即使我很长时间没有更改发出警告的代码,我也不明白为什么会出现。
原因:"Generic iOS Device"。
选择设备或模拟器时,警告消失。
仍然,我将“(long)”添加到 NSLog 变量中。直到那时我只有“%ld”,无论选择什么(通用设备、真实设备、模拟器)都会打开警告
我收到以下代码行的警告:
NSLog(@"selected segment: %li", _segmentControl.selectedSegmentIndex);
属性 selectedSegmentIndex
的类型是 NSInteger
。
如果我将格式更改为 %i
,我会收到以下警告:
Format specifies type 'int' but the argument has type 'long _Nullable'
可空类型是否有任何新的格式说明符,或者这只是 Xcode 7 中的一个错误?
你应该输入:
NSLog(@"selected segment: %li", (long)_segmentControl.selectedSegmentIndex);
因为 NSInteger 在 32 位和 64 位架构中的长度不同。以前您没有看到警告,因为您可能只针对 64 位架构进行编译。
我还建议阅读 Apple Article,因为 Xcode 7 中有新的说明符(以及 nullable
和 nonnull
)。
要回答您对评论的疑问,请参阅此 Apple document,其中说明如下:
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);
只想补充: 我收到这个警告 "suddenly" 即使我很长时间没有更改发出警告的代码,我也不明白为什么会出现。
原因:"Generic iOS Device"。 选择设备或模拟器时,警告消失。
仍然,我将“(long)”添加到 NSLog 变量中。直到那时我只有“%ld”,无论选择什么(通用设备、真实设备、模拟器)都会打开警告