前言: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两个警告来保证没有严重错误,当然,如果有些明显的不会出错,可以用关闭某个或者某些警告。

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="box-sizing: border-box;">-Wall</span> <span class="hljs-attribute" style="box-sizing: border-box;">-Wno</span><span class="hljs-attribute" style="box-sizing: border-box;">-unused</span><span class="hljs-attribute" style="box-sizing: border-box;">-variable</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//启用Wall但是剔除unused-variable</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

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

强制开启一个警告

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">warning</span> "This method can not be used"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

强制开启一个错误

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">error</span> "You must add this key,or you will fail"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

强制关闭一个警告 

例如 

这里会出现警告test这个selector没有实现

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">performSelector:</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@selector</span>(test) <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">withObject:</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nil</span>];
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

强制关闭这个警告

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">pragma</span> clang diagnostic push</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">pragma</span> clang diagnostic ignored "-Wundeclared-selector"</span>
[self performSelector:@selector(test) withObject:nil];
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">pragma</span> clang diagnostic pop</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

常用的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/

原文地址:http://blog.csdn.net/hello_hwc/article/details/46425503

iOS XCode启用/关闭Clang Warnings的更多相关文章

  1. Ubuntu中启用关闭Network-manager网络设置问题!

    Ubuntu中启用关闭Network-manager网络设置问题! [Server版本] 在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/net ...

  2. 启用“关闭自动根证书更新”,解决Windows系统各种卡顿的问题(Visual studio 卡、远程桌面mstsc卡、SVN卡)

    最近,发现在Win7下面一系列操作都会出现卡顿的情况: 1.  Visual studio 启动调试和关闭调试时,都会卡上半分钟左右 2.  使用远程桌面mstsc.exe,点击连接时,也会卡上半分钟 ...

  3. [转]phonegap 2.9 IOS Xcode 搭建环境

    phonegap 2.9 IOS Xcode 搭建环境   一:下载phoneGap2.9和安装Xcode5(目前最新版) 选择2.9是因为3.0以上坑爹版本编译神马的要在有网络情况. 二: 下载ph ...

  4. iOS Xcode及模拟器SDK下载

    原文: Xcode及模拟器SDK下载 如果你嫌在 App Store 下载 Xcode 太慢,你也可以选择从网络上下载: Xcode下载(Beta版打的包是不能提交到App Store上的) 绝对官方 ...

  5. iOS 开发之 - 关闭键盘 退出键盘 的5种方式

    iOS 开发之 - 关闭键盘 退出键盘 的5种方式   1.点击编辑区以外的地方(UIView) 2.点击编辑区域以外的地方(UIControl) 3.使用制作收起键盘的按钮 4.使用判断输入字元 5 ...

  6. iOS/Xcode异常:no visible @interface for XXX declares the selector YYY

    在iOS/Xcode开发过程中,出现如下异常信息: no visible @interface for XXX declares the selector YYY 分析原因: There are lo ...

  7. iOS—Xcode 7真机测试

    Xcode 7真机测试详解 1.准备 注意:一定要让你的真机设备的系统版本和app的系统版本想对应,如果不对应就会出现一个很常见的问题:could not find developer disk im ...

  8. 在Xcode中使用Clang Format

    Xcode中的Re-Indent,顾名思义,只是一个调整缩进的功能,完全依赖它来进行代码格式化显然不够用.我们使用了一个叫做ClangFormat-Xcode的插件,配合Re-Indent一起来做代码 ...

  9. < IOS > X-code 5.1 x86 - 64 编译问题

    关于xcode 5.1   x86 - 64 编译问题   坐等了N久,终于IOS 7.1 发布了,作为一个果粉,忍不住第一时间升级了.结果用设备测试的时候,出问题了,一直检测不到设备,哈哈,纠结了半 ...

随机推荐

  1. RAM和ROM总结

    RAM和ROM总结 一.在解释之前先备注一些缩写的全称便于记忆: 1.EPROM:(Electrically Programmable Read-Only-Memory)电可编程序只读存储器 2.EE ...

  2. 对DIP IoC DI的理解与运用

    DIP,IoC,DI基本概念 依赖倒置原则(DIP,Dependency Inverse Principle):强调系统的“高层组件”不应当依赖于“底层组件”,并且不论是“高层组件”还是“底层组件”都 ...

  3. [C++][数据结构]队列(queue)的实现

    对于队列的定义,前人之述备矣. 队列的实现方法与栈非常相似.我直接在我实现的那个栈的代码上加了一点东西,全局替换了一些标识符,就实现了这个队列. 我实现的是一个queue<value>容器 ...

  4. Can't find PHP headers in /usr/include/php

    解决办法: yum install php-devel

  5. curl运行json串,代理转发格式

    curl -b 'uin=o0450654733; skey=@tq9xjRvYy' -H "Content-Type: application/json" -X POST -d ...

  6. 分布式缓存技术memcached学习(四)—— 一致性hash算法原理

    分布式一致性hash算法简介 当你看到“分布式一致性hash算法”这个词时,第一时间可能会问,什么是分布式,什么是一致性,hash又是什么.在分析分布式一致性hash算法原理之前,我们先来了解一下这几 ...

  7. C#中UnixTime和DateTime的转换(转载)

    由于在API请求中返回回来的时间格式为UNIX形式,需要转换成正常的显示方式,在网上找到了这么一个例子. 使用是在C#中使用的,所以WP8开发应该也可以. 转载源地址:http://blog.linu ...

  8. C# CodeFirst(EF框架)代码优先创建数据库

    namespace WebEF.Model{ public class ModelContext:DbContext //继承DBcontext 来自EF框架 { public ModelContex ...

  9. [Android]新版的sdk中新建一个android应用,增加的PlaceholderFragment这个静态类发生的事情

    1,首先发生的是有两个布局xml,一个activity_main.xml,一个是fragment_main.xml一开始没在意,后来仔细看了原来是新功能的fragment概念等于多个场景在这个acti ...

  10. Java的算数运算符、关系运算符、逻辑运算符、位运算符

    JAVA的运算符,分为四类: 算数运算符.关系运算符.逻辑运算符.位运算符 算数运算符(9):+  -  *  /  %  ++  -- 关系运算符(6):==  !=  >  >=  & ...