The original links: http://ios.jobbole.com/88643/ the author: what are behind that Pluto – * * Y study just for my personal collection, * * the original blogger if you don’t agree to delete, please contact qq651263878 here thank you and apologize for any inconvenience caused.

Oh, my god. Are those the only two things we need to talk about? OC or Swift developers know what’s going on, okay? Isn’t that what you do for marking and grouping code? Are there any other fake skills?

Of course, in fact, ask most people to say what are these two functions, or in addition to these two know what the situation. Most people only know that these two are used to organize code. While this is true and false, it is true that we often use these two methods to organize code, but many of you who have looked at open source have certainly seen this form of code:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles". // Some code#pragma clang diagnostic pop
Copy the code

So what is this code for? Just listen to me slowly.

It’s true that in our everyday work we often use #pragma mark or // mark: to organize code, and we all know that organizing code takes two forms: one for grouping and one for tagging. How to show the specific you also when I nonsense, look at the following code:

// Objective-C code - (void)viewDidLoad {[super viewDidLoad]; [self initAll]; }#pragma mark Custom functions
- (void)initAll {
    ...
}

#pragma mark - UITableViewDataSources
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Click on the %li line", indexPath.row);
}
Copy the code

Here is the Swift code:

// Swift code override funcviewDidLoad() {  
    super.viewDidLoad()
    initAll()
}

// MARK: Custom function
private func initAll() {... } // MARK: - UITableViewDataSource func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return 10
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {  
    return UITableViewCell(style: .Default, reuseIdentifier: "CELL")
}

// MARK: - UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {  
    print("Click on line \(indexPath. Row)")}Copy the code

The effect, as you all know, looks like this:

I won’t say much about how to use this, but try it out, and most of you know this feature. So let’s talk about the other features around this. First, let’s talk about a powerful device that can be used to locally control the on/off of a certain warning. This is the one we mentioned at the beginning of this article:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"// a piece of code#pragma clang diagnostic pop
Copy the code

This code, in fact, is used to turn off warnings, which is especially useful when you need to ignore some warnings when writing some code. Let’s try it out with the one above.

#pragma mark - UITableViewDataSources
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
    int unusedInt;
#pragma clang diagnostic pop
    return 10;
}
Copy the code

Ignore warnings that the unsedInt attribute is not used with this code. How do I know what the string “-wunsed-variable” is? Want to know? Guess what? I didn’t tell you I didn’t tell you! I didn’t tell you! (Pia ~ a shoe come over, this is three days do not hit the roof jie tile!) Clang Warning Is Generating This Message? , which contains a number of warning suppression strings, you can refer to. Another solution is to search through Xcode, as shown in the following figure:

Select the Report Navigator first, then select the last log, select all the information, and then go to the log to see the warning information you need, and it will list the warning information wherever you need it. In this way, we can force the #pragma Clang Diagnostic ignored message to be added.

Of course, for the above special use of the warning, you can also use the following form to turn off the warning:

// Objective-C code NSString *unsedString;#pragma unused(unsedString)
Copy the code

Ok, now that you have learned this trick, you can install the pussy in your own code, so what are the other useful features in OC? We can use the following code to manually generate warnings:

#pragma message "This is a warning"
#warning "This is another warning"
Copy the code

What are the specific effects? Take a look at the chart below:

Let’s talk about what we can use in Swift besides MARK: // TODO: // FIXME: // FIXME: // TODO: // FIXME: // TODO: // FIXME: // TODO: // FIXME: // TODO: // FIXME: //