iOS XCode启用/关闭Clang Warnings

来源:互联网 发布:linux饥荒服务器搭建 编辑:程序博客网 时间:2024/06/03 22:06

ios



原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
欢迎关注我的iOS SDK详解专栏,这里有很多基础的文章。
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html


前言:warnings是编码中很重要的一个环节,编译器给出合理的warning能帮助开发者找到自己代码的问题,防止很多bug产生。
默认用XCode创建一个工程,会自动开启一些重要的warnings,但是更多的时候,我们需要编译器更完整的提醒。

iOS开发采用Clang编译器


默认的Warning可以在Build Settings里找到

在search里搜索Warnings,就可以看到如图,这是为所有语言开启的warnings
技术分享

当然,也可以为不同语言开启warning,也在Build Settings里
技术分享

但是,这样一个个的开启关闭定制化很好,有时候我们只需要开启全部或者开启全部重要的warnings即可
这时候,进入
技术分享
可以添加一些build flag来启用警告,主要的就是三个

1.-Wall Clang认为自己能够准确报出的警告
2. -Wextra额外的苛刻的警告,这些警告不一定会造成错误。例如如果使用这个flag,把singned 赋值给unsigned就会触发警告,而大多数时候这样赋值是没问题的。
3.-Weverything 所有警告

一般的项目都是开启-Wall-Wextra两个警告来保证没有严重错误,当然,如果有些明显的不会出错,可以用关闭某个或者某些警告。

-Wall -Wno-unused-variable //启用Wall但是剔除unused-variable

用语句强制开启或者关闭某个警告

强制开启一个警告

#warning "This method can not be used"

强制开启一个错误

#error "You must add this key,or you will fail"

技术分享

强制关闭一个警告
例如
这里会出现警告test这个selector没有实现

[self performSelector:@selector(test) withObject:nil];

强制关闭这个警告

#pragma clang diagnostic push#pragma clang diagnostic ignored "-Wundeclared-selector"    [self performSelector:@selector(test) withObject:nil];#pragma clang diagnostic pop

常用的CLang Warning

  • Wall
  • Wbad-function-cast
  • Wcast-align
  • Wconversion
  • Wdeclaration-after-statement
  • Wdeprecated-implementations
  • Wextra
  • Wfloat-equal
  • Wformat=2
  • Wformat-nonliteral
  • Wfour-char-constants
  • Wimplicit-atomic-properties
  • Wmissing-braces
  • Wmissing-declarations
  • Wmissing-field-initializers
  • Wmissing-format-attribute
  • Wmissing-noreturn
  • Wmissing-prototypes
  • Wnested-externs
  • Wnewline-eof
  • Wold-style-definition
  • Woverlength-strings
  • Wparentheses
  • Wpointer-arith
  • Wredundant-decls
  • Wreturn-type
  • Wsequence-point
  • Wshadow
  • Wshorten-64-to-32
  • Wsign-compare
  • Wsign-conversion
  • Wstrict-prototypes
  • Wstrict-selector-match
  • Wswitch
  • Wswitch-default
  • Wswitch-enum
  • Wundeclared-selector
  • Wuninitialized
  • Wunknown-pragmas
  • Wunreachable-code
  • Wunused-function
  • Wunused-label
  • Wunused-parameter
  • Wunused-value
  • Wunused-variable
  • Wwrite-strings

困难模式

所谓的困难模式就是开启所有警告,并且把警告当作error。这意味着,只要还有一个警告存在,那么程序将不能编译运行。
技术分享


参考链接
http://onevcat.com/2013/05/talk-about-warning/
http://programmers.stackexchange.com/questions/122608/clang-warning-flags-for-objective-c-development
http://amattn.com/p/better_apps_clang_weverything_or_wall_is_a_lie.html
http://nshipster.cn/clang-diagnostics/


0 0
原创粉丝点击