我们首先要搞清楚的是:作为预处理指令,#pragma 在编译时进行计算。但它并不像如 #ifdef…#endif 之类的宏,#pragma 的使用方式不会改变你的应用运行时的行为。相反的,#pragma 声明主要由 Xcode 用来完成两个主要任务:整理代码和防止编译器警告。
我相信大家都用过 #pragma mark来在划分代码模块,使代码更整洁、逻辑更清晰。例如:
@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:(