#pragma进阶
来源:互联网 发布:剑三温婉成女捏脸数据 编辑:程序博客网 时间:2024/06/04 18:06
对于#pragma的使用,最初的设计是为了使源代码在不同的编译器下能够兼容,而xcode将#pragma的作用发挥到了极致。#pragma是一个预处理命令,是在编译时进行计算的。
综合起来#pragma的作用主要就两个
- 整理代码
- 防止编译警告
整理代码
说到这里,就是如何写出整洁优雅的代码了。#pragma算是其中比较重要的一个命令。直接上代码
@implementation ViewController- (id)init { ...}#pragma mark - UIViewController- (void)viewDidLoad { ...}#pragma mark - IBAction- (IBAction)cancel:(id)sender { ...}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ...}#pragma mark - UITableViewDelegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ...}
这样的书写格式,我们可以清晰的找到项目功能比较紧密的代码块,同时通过xcode我们也能迅速的索引到相应的代码块。
防止编译警告
有时候我们在引用第三方的源代码或者静态库的时候会发现有时候会有很多warnning,很烦人。这个时候#pragma派上用场了,用下面的方式就可以去除掉警告了
// 消除方法弃用的警告#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" [TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]]; #pragma clang diagnostic pop
// 消除不兼容指针类型警告#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wincompatible-pointer-types" // #pragma clang diagnostic pop
// 消除循环引用的警告 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-retain-cycles" self.completionBlock = ^ { ... }; #pragma clang diagnostic pop
// 消除未使用变量#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" int a; #pragma clang diagnostic pop
常用到的可能就这些,如果需要了解更多的消除警告的命令可以去http://fuckingclangwarnings.com/这里看看。虽然消除警告好用,但也不能老用,毕竟警告说明我们的代码在某种程度上是存在或多或少的问题的。想办法解决才是正途,实在解决不了了再考虑这种方法。要不总给人一种讳疾忌医的感觉。如果想要详细了解你可以在Clang Compiler User’s Manual读到更多关于#pragma的LLVM用法的知识。
好了,对于#pragma的讲解就到这了,希望通过#pragma能够对提升你的代码有帮助。
2 0
- #pragma进阶
- #pragma
- Pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- #pragma
- Qt的designer在win10下无法启动的问题
- c语言中为何提出了指针的概念?
- 2016.2的ideaUI注册码
- Python 的 input() 与 raw_input()
- 如何安装OCSNG及GLPI
- #pragma进阶
- 以springMVC+spring+hibernate为框架的项目搭建步骤
- 卷积神经网络CNN、caffe_Blob(improving)
- 如何使用万能地图下载器下载有偏移的谷歌卫星地图
- 如何使用引号赋值
- 在linux上安装flash player插件
- springmvc 使用jsp生成pdf非ajax提交
- Codeforces735A Ostap and Grasshopper
- zurmo(七)-部分报错解决(1)