#pragma介绍
#pragma
is a preprocessing directive, the Chinese meaning of pragma is "compiling instructions". It's not something that's unique in objective-c (it seems to be used more in C + +), and it was originally designed to ensure compatibility between different compilers, but over time it appeared in more and richer scenarios.
#pragma
The code is processed during compilation, is neither a comment nor part of the logical code, and unlike other preprocessing commands #ifdef ... #endif
, it does not affect the logic of the code at run time, so the #pragma
instruction does not affect the performance of the program at run time. According to the description of Mattt Thompson God, there are two scenarios in the current Xcode development environment #pragma
: Organization code and masking compilation warnings.
#pragma mark Organization Code
Organization code is a personal hygiene problem, poor personal hygiene (poor code organization) does not reflect the character (technical ability), but it in some way affects whether others are willing to engage with him (cooperation). In a project (especially multi-person cooperation projects), there should be a better internal consistency coding habits, bad habits or lack of consistency, will make the project difficult to maintain, collaboration is also inconvenient (this is the author's deep experience, the author of the project has a colleague, coding ability is fair, but coding habits is too bad, The code is compact, never space, even more to talk about empty lines and comments, and even often misaligned, look at his code, a little modification of the desire to have no, want to just rewrite it again =_=).
When you organize your code, it's an #pragma mark
important part of writing "clean code," like this:
#pragma mark-uiviewcontroller- (void) Viewdidload {[Super Viewdidload];} - (void) Dealloc {}#pragma mark-ibaction- (ibaction) Loginnow: (ID) sender { }#pragma mark-uitableviewdatasource- (Nsinteger) TableView: (uitableview *) TableView numberofrowsinsection: (nsinteger) Section { return 0; }#pragma mark-uitableviewdelegate- (void) TableView: (uitableview *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath { }
Using the code in #pragma mark
the above code (method) into a few logical sections, this processing does not change the program logic, but we can use the Xcode Code navigation tool to visually draw a moment of pleasure: @implementation
What is the basis for organizing n methods for a section? This is a matter of opinion. Generally speaking:
- A protocol method is organized into a section;
- The Target-action type method is organized into a section;
- Notification related methods are organized into a section;
- The parent method that requires override is composed into a section;
#pragma屏蔽编译警告
Using #pragma mark
to organize code usage is relatively common, and the #pragma
warning of using directives to mask the compiler and static analyzers is comparatively new.
What are even more annoying than poorly-formatted code know? Code that generates warnings. Compile warnings are hateful, you should modify the code as much as possible to kill these warnings, but sometimes some warnings can not be avoided, such as when we write @selector(aMethodName)
such code, if Amethodname does not appear in the context, may appear containing "Undeclared-selector" The key word of warning, a neat-and-tidy programmer will think of killing this warning, at this time the #pragma
instructions come in handy. Such as:
#pragma clang diagnostic push#pragma clang diagnostic ignored "-wundeclared-selector" if ([self. Selectedviewcontroller respondstoselector:@selector (isreadyforediting)]) { Boolnumber = [self. Selectedviewcontroller performselector:@selector (isreadyforediting)]; }#pragma clang diagnostic pop
This is a solution provided by the Clang compiler that #pragma clang diagnostic push/pop
tells the compiler to ignore specific warnings only for a specific part of the code (remember to use pop at the end of the snippet to restore the initial diagnostic settings).
The following example #pragma clang diagnostic ignored
indicates a "-Wundeclared-selector"
"specific warning," and then the other scenario is swollen, do you know what to fill in? The network is powerful, there is a (probably N) angry netizens to clang warning message made a summary, the reason that this netizen "angry", because its link is very interesting: http://fuckingclangwarnings.com/
Finally, you can read more about the LLVM's use of #pragma in the Clang Compiler User ' s Manual.
Finally, thanks to the great god mattt Thompson, this article's reference is entirely from the "#pragma", originally wanted to use their own language to complete this blog, while reading and writing, finally found almost nearly, did not leave anything of their own, so it!
References
- "#pragma";
- "Clang diagnostics";
preprocessing Directive #pragma