objective-c – Xcode警告:Format指定类型’long’但参数的类型为’int _Nullable’

前端之家收集整理的这篇文章主要介绍了objective-c – Xcode警告:Format指定类型’long’但参数的类型为’int _Nullable’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到以下代码行的警告:

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

selectedSegmentIndex属性的类型为NSInteger.

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

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

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:

06001

猜你在找的Xcode相关文章