我收到以下代码行的警告:
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:
NSInteger i = 34; printf("%ld\n", (long)i);
关于objective-c - Xcode 警告 : Format specifies type 'long' but the argument has type 'int _Nullable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33121583/