如何删除更新到 arm64 后收到的 100 条警告 "implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'"?

How to remove 100s of warnings "implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'" I got after updating to arm64?

问题:

昨天我将我的一个大型项目转换为支持 arm64 之后,我立即收到了 500 多个警告。其中大约 70% 是 NSInteger 被分配给 int 的地方,反之亦然,其余的是 NSUInteger 在 NSString 中格式化的地方,如下所示:

NSInteger a = 123;
NSString *str = [NSString stringWithFormat:@"Int:%d", a]; //warning: value of 'NSInteger' should not be used as formate argument; add an explicit cast to 'unsigned long' instead.

现在我知道如何手动给它们寻址了,但这是一项艰巨的任务,非常费力。 我也知道我可以将类型不匹配警告全部静音,但我不想那样做。当然,他们很有帮助。

我试过的:

  1. 我已经使用 find-n-replace 将 [NSNumber numberWithInt:abc]; 转换为 [NSNumber numberWithInt:(int)abc];。它修复了一些。
  2. 我还尝试将我所有的 int 属性更改为 NSInteger 属性 但它使警告数量翻了一番(达到 900+ 次)。所以我 恢复。

  3. 我也试过找一些正则表达式但是没找到 适合我需要的东西。

问题:

我正在寻找正则表达式或有人尝试过的任何其他解决方法,它们可以减少手动修复它们所需的工作量。

提前致谢。

objective c 隐式转换丢失整数精度 'NSUInteger'(又名 'unsigned long')到 'int

在项目 > 构建设置中更改密钥 "implicit conversion to 32Bits Type > Debug > *64 architecture : No"

其他警告

在项目 > 构建设置中更改密钥 "typecheck calls to printf/scanf : NO"

说明:[工作原理]

检查对 printf 和 scanf 等的调用,以确保提供的参数具有适合指定格式字符串的类型,并且格式字符串中指定的转换有意义。

希望有用

[注意:这可能会导致 64 位架构转换的其他警告无效]。

NSInteger a = 123;
NSString *str = [NSString stringWithFormat:@"Int:%ld", (long)a];

更新到 64 位后需要像这样进行类型转换((long)a)。 %d 仅适用于长整数的 32 位范围 %ld。为了更好地理解,请阅读此苹果文档。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Cocoa64BitGuide/ConvertingExistingApp/ConvertingExistingApp.html

如果其他人也面临类似情况,我想说明如何处理。尽管@Raju 的回答建议手动执行(我想避免这样做),但我在他分享的 link 中找到了我需要的东西。

Apple 提供了一个名为 ConvertCocoa64 的 64 位转换脚本,位于 /Developer/Extras/64BitConversion/ConvertCocoa64,它不仅将所有 int 转换为 NSInteger,它还处理 float 到 CGFloat 的转换,如所述:

It converts most instances of int and unsigned int to NSInteger and NSUInteger, respectively. It doesn't convert ints in bit-field declarations and other inappropriate cases. During processing, the script refers to a hardcoded list of exceptions.

除了上述转换之外,它还会标记代码中需要手动修复的行。所以这可能有助于字符串格式的警告。

请参阅this link了解完整详情。它不仅解释了如何使用脚本,还建议了一些非常重要的 post 64 位迁移检查点。