首先, #pragma 本质上也是声明,一般常用的功能就是打注释、尤其是分段注释
但是#pragma 另外一个强大的功能就是处理编译器警告,用的时候可能就没上一个
功能用的那么多,在代码中处理警告却是极其高效的方法。
其中 clang diagnostic 便是#pragma 第一个功能常用的命令,步骤如下
1
2
3
4
|
#pragma clang diagnostic push
#pragma clang diagnostic ignored
"-相关命令"
// 你自己的代码
#pragma clang diagnostic pop
|
常见用法
1.方法弃用告警
1
2
3
4
5
6
|
#pragma clang diagnostic push
#pragma clang diagnostic ignored
"-Wdeprecated-declarations"
[TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]];
#pragma clang diagnostic pop
|
2.不兼容指针类型
1
2
3
4
|
#pragma clang diagnostic push
#pragma clang diagnostic ignored
"-Wincompatible-pointer-types"
//
#pragma clang diagnostic pop
|
3.循环引用
1
2
3
4
5
6
7
|
// completionBlock is manually nilled out in AFURLConnectionOperation to break the retain cycle.
#pragma clang diagnostic push
#pragma clang diagnostic ignored
"-Warc-retain-cycles"
self.completionBlock = ^ {
...
};
#pragma clang diagnostic pop
|
4.未使用变量
1
2
3
4
|
#pragma clang diagnostic push
#pragma clang diagnostic ignored
"-Wunused-variable"
int
a;
#pragma clang diagnostic pop
|
#pargma 用法详情:
http://nshipster.cn/pragma/
http://nshipster.com/clang-diagnostics/
相关的命令列表
http://fuckingclangwarnings.com/
进阶:http://clang.llvm.org/docs/UsersManual.html#diagnostics_pragmas