Xcode 警告:格式指定类型“long”,但参数类型为“int _Nullable”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33121583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:03:09  来源:igfitidea点击:

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

objective-cxcodexcode7

提问by bbjay

I get this warning for the following line of code:

我收到以下代码行的警告:

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

The property selectedSegmentIndexis of type NSInteger.

该属性selectedSegmentIndex为 类型NSInteger

If I change the format to %ii get the following warning:

如果我将格式更改为%i我收到以下警告:

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?

Nullable 类型是否有任何新的格式说明符,或者这只是 Xcode 7 中的一个错误?

回答by Vive

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.

因为 NSInteger 在 32 位和 64 位架构中的长度不同。以前您没有看到警告,因为您可能只针对 64 位架构进行编译。



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

我还建议阅读Apple 文章,因为 Xcode 7 中有新的说明符(以及nullablenonnull)。



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

要从评论中回答您的疑问,请参阅此Apple 文档,其中说明了以下内容:

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

类型说明符

脚本操作:警告潜在问题;可能会产生假阴性。

通常,在 32 位代码中,您在 printf、NSAssert 和 NSLog 等函数以及 stringWithFormat: 等方法中使用 %d 说明符格式化 int 值。但是对于 NSInteger,它在 64 位体系结构上的大小与 long 相同,您需要使用 %ld 说明符。除非您像 64 位一样构建 32 位,否则这些说明符会在 32 位模式下生成编译器警告。为避免此问题,您可以根据需要将值强制转换为 long 或 unsigned long。例如:

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

回答by user1105951

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.

原因:“通用iOS设备”。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)

仍然,我将 NSLog "(long)" 添加到变量中。直到那时我只有“%ld”,无论选择什么都会发出警告(通用设备,真实设备,模拟器)