38

【iOS】去除Xcode代码警告

 4 years ago
source link: https://www.tuicool.com/articles/Rbq6Jfe
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

写在前面

我们的工程中,一般都会有很多警告,当然了,一般这些警告都是可以忽略的,不会影响程序正常执行。

但是有些人就是看着这些警告不爽,嗯,很不爽。于是会想方设法去除这些警告。

最好的办法,当然是按照Xcode给的警告,老老实实地替换代码实现的方式或者按照Xcode所想的那样进行处理。比如使用了过时的API,那就替换成新的API实现。

但是总会有一些原因使得我们没办法通过这种方式去除警告,于是苹果就给了另外一种方式让我们忽略掉这些恶心的警告。

去除Xcode代码警告的方法

我们只需要将引起警告的代码用

#pragma clang diagnostic push
#pragma clang diagnostic ignored "警告的类型"

#pragma clang diagnostic pop

包裹起来就行了。

举个例子,下面的代码Xcode会显示警告:

2eUZNbB.png!web

button.font过时警告

使用我们刚才将的方式将引起警告的代码包裹起来,就能顺利干掉这个警告了:

ZzEvmaA.png!web

去除警告

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    button.font = [UIFont systemFontOfSize:30];
#pragma clang diagnostic pop

查找警告的类型

那么,我如何知道 警告的类型 是什么呢?

这里有一个比较简单的方法:

build -> 找到buildtime界面 -> 选中对应的警告 -> 右键 -> 选择Reveal in Log -> 就能找到对应的警告类型了。

NbMfIrI.png!web

找到警告类型的步骤

If6raaB.png!web

很容易就找到对应的警告类型了

如上图,我们点击了  Reveal in Log  之后,会跳转到该界面,并且Xcode会自动闪量一下对应的警告的描述,在描述的后面就会找到警告的类型了。

参考


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK