项目警告处理

Published on

今天不经意一看,项目中有上千个警告了,虽然没有强迫症,但是看着也是觉得不舒服,刚好手头上的活不多,干脆就清理一下,记录一下过程。

消除警告

在清理之前,在网上查找资料发现有几种通用的方式消除警告。

全局忽略警告

  1. 选中警告,右键选择reveal in log。

A99CE568-B37E-4085-ACB4-E84596CB0330.png

  1. 找到黄色加重的警告后面中括号中的标识符

3BF6D1CC-CB83-4307-B314-DF13422B8623.png

  1. 选择project(或者是targets),选中build Settings,搜索 other warning flags

  2. 将中标识符填进去,注意在-W之后需要添加no-,使之不提示。

    0C072371-FC52-4CBA-917C-834422667991.png

  3. 重新编译

单独忽略

  1. 按照全局忽略的1、2步获取到标识符。
  2. 使用宏包住报警告的代码,注意ignored后面直接填写标识符,不需要加no-
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wunused-variable"

               //这里是会报警告的代码

#pragma clang diagnostic pop

3.重新编译

注意:关于project和targets的选择,有些类型的警告只能在project中的build Settings中添加才有效,有些则是需要在target中添加才有效,例如Unused variable,如果项目中只有一个target,那么可以在project和target中都加上。


一、pod文件警告去除

这个简单,在podfile中加一行指令就行。

inhibit_all_warnings!

二、warning: directory not found for option

  1. 选择工程, 编译的 (targets)
  2. 选择 Build Settings 菜单
  3. 查找 Library Search Paths 和 Framework Search Paths,删掉编译报warning的路径即可。

注意:要是路径太多,可以试试删除掉Library Search Paths和Framework Search Paths所有路径,然后重新pod install再编译。(根据项目情况而定)

三、this function declaration is not a prototype

函数没有申明警告或者在block中没有带参数在xcode9中也会提示警告。

解决方法:

  • typedef void (^UpdateSwichBtnBlock)(void); 每一个警告的地方加上void
  • 在project中选择 Build Settings 菜单,查找strict prototypes,改成NO。

四、empty paragraph passed to ‘\param’ command

很多时候在注释中会有很多警告,是因为没有填写完整方法的参数信息,这种警告一般不重要,可以使用全局忽略方法来取消。

五、Treating Unicode character as whitespace

可能是因为粘贴代码时的回车和空格带来的格式错误,删除重新输入就好了。

六、Undeclared selector ‘historyAction’

使用未声明的方法,一般出现在@selector() 括号里写了个不存在的方法或方法名写错了。

七、Null passed to a callee that requires a non-null argument

这是xcode为了让oc和swift兼容而推出的可选功能,如果需要全部忽略可以使用通用方法。-Wno-nonnull

八、Convenience initialzer missing a self call to another initializer

Xcode编译提醒/指定初始化器问题,可以用编译器指令来屏蔽

20160125180401115.png

#pragma clang diagnostic ignored "-Wobjc-designated-initializers"

九、‘strongify’ macro redefined

宏申明重复,删除一个就ok。

十、一些常见警告

我们项目中最常见的应该就是一下几种类型的警告,并不建议忽略,因为对于代码检查和重构比较重要,我是在需要解决其他警告的时候,先把这些烦人的警告隐藏起来,处理完其他特殊的警告再放出来。

  • Unused variable ‘*’ 没有使用 [-Wunused-variable]
  • Code will never be executed 代码永远不会执行 [-Wunreachable-code]
  • deprecated: first deprecated in iOS 8.0 方法已废弃 [-Wdeprecated-declarations]
  • is only available on iOS 11.0 or newer 使用了超过适配版本的方法 [-Wunguarded-availability-new]

添加警告

普通警告

在某些位置需要之后修改,或者需要提醒别人时,可以自己加一警告防止忘记。

#warning TODO

废弃警告

如果是自己写的文件或库,有新的接口需要废弃之前的接口时,可以使用废弃警告。

  • 不带信息的警告

- (void)addTapAction:(SEL)tapAction target:(id)target NS_DEPRECATED_IOS(2_0, 4_0);

  • 带信息的警告

- (void)addTapAction:(SEL)tapAction target:(id)target __attribute((deprecated("这个接口已经废弃")));


国外的一位朋友将大部分的警告都总结了对应的标识符,有需要的可以去看一下。Which Clang Warning Is Generating This Message?