objective-c - Xcode 警告 : Format specifies type 'long' but the argument has type 'int _Nullable'

标签 objective-c xcode xcode7

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

    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 中有新的说明符(其中包括 nullablenonnull)。


解答你评论中的疑惑,请引用这个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/

相关文章:

ios - 没有缺口的 iPhone X 模拟器截图

ios - 链接器命令失败,退出代码为 1 - Xcode 7

objective-c - 许多功能已弃用 - iOs 6

ios - 在构建时分配一个静态常量 ios xcode

ios - Xcode Unit testing : class is implemented in both. 两者之一会用到xctest

使用#if、#endif 的 Swift 错误

ios 自动布局约束问题

autolayout - Xcode 7 添加尾随/前导/顶部/底部约束

objective-c - 从 Objective-C 中的数组中消除连续重复项的最快方法

ios - 如何检查是否给出了正确的链接