Talk about #pragma and//MARK:

Source: Internet
Author: User

I'm going to talk about these two things? are OC or swift developers all know what's going on, okay? is that what you use to tag and group code? Do you have any other skills to install?

Of course, the fact is that most people say what the two are, or what they know. Many people only know that these two are used to organize the code. However, it is also wrong to say that it is true that we often use these two to organize the code, but if you have seen a lot of open source people must have looked at the form of code:

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-warc-retain-cycles"

.... //Some code

#pragma clang diagnostic pop

So what do these codes do for me? And listen to my slow way.

It is true that in our daily work, we often use #pragma mark or//mark: To organize the code, and everyone knows that there are two forms of organization code: one for grouping and one for labeling. Concrete how to show everyone 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 ten;

}

- (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(@"clicked%li Line", indexpath. Row);

}

Here is the SWIFT code:

Swift Code

Override func Viewdidload() {

Super. Viewdidload()

initall()

}

Mark:custom function

Private func initall() {

...

}

MARK:-Uitableviewdatasource

Func tableView(tableView: uitableview, numberofrowsinsection section: Int ) , Int {

return ten

}

Func tableView(tableView: uitableview, cellforrowatindexpath Indexpath : nsindexpath) , UITableViewCell {

return UITableViewCell(style: . Default, reuseidentifier: "CELL")

}

MARK:-Uitableviewdelegate

Func tableView(tableView: uitableview, didselectrowatindexpath Indexpath : nsindexpath) {

Print("clicked \ (indexpath. row)

}

So the effect, as we all know, is this:

In fact, the main point is two, one is not followed by the '-' situation, this situation will not appear grouped, just when there is a comment, and the other is the case with '-', then this situation will add a horizontal split line, that is, there are groups. It is best to divide the code into the same group when we write the code. In this way, the code can be read and understood by the people who maintain it later. This is what we often say to make comments, to increase the readability of the code.

The use of this is not much to say, everyone to try, and most of them know this feature. So we're going to talk about other features around this, first of all, let's talk about an artifact that can be forced, that is, using it to partially control the opening and closing of a warning, which is what we say at the beginning of the article:

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-wunused-variable"

A section of code

#pragma clang diagnostic pop

It is estimated that many small partners often see this code in many open source code, which is actually used to turn off warnings, which is especially useful when you need to ignore certain warnings when you write some code. For specific use, let's try this one.

#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 ten;

}

The following code ignores warnings that do not use the Unsedint property. Then the question came, and someone asked, "How do I know what the string in the back of the-wunsed-variable should be?" You want to know? Guess what, don't tell you, don't tell you, don't tell you! (pia~ one take off the shoes, this is three days do not hit the roof!) )

Well, there are two ways, one is that someone on the internet has organized a website which Clang Warning is generating this Message?, which contains a number of warning suppression strings, you can refer to. Another option is to use Xcode to find the specific steps such as:

http://fuckingclangwarnings.com/

Select the report Navigator, then select the last log, select all the information, and then go to the log to see the warning you need, and where to warn it will be listed. By this means, we can add #pragma clang diagnostic ignored information.

Of course, for this particular use warning, you can turn off the warning in the following form:

Objective-c Code

NSString *unsedstring;

#pragma unused (unsedstring)

Well, learn this trick can be installed in their own code, then we are talking about OC in what are the useful features? Actually speaking of warning, then we can say how to manually produce a warning, there are two ways, specifically, you can see the following code:

#pragma message "This is a warning"

#warning "This is another warning"

What is the effect of the concrete? Take a look at the following picture:

It is not surprising, in fact, in our daily work can through this code for the moment is not ready to fill in the content of the place to play a warnings, and then we can in the process of writing code to notice these warning, so we can come to pits. After all, I am a person with code cleanliness, there are a lot of warnings in my code is completely unbearable, I hope you can develop this habit. In fact, in addition to the warning, there is a #error can be used, we can try.

Let's talk about what we can do with Swift in addition to mark: there are actually two things we can use, in which we can use/TODO: with//FIXME: equivalent to a warning in OC, I prefer this usage in swift, Because its purpose is clearer and its usage is consistent with Mark, the specific usage and effects look like this:

This is the end of the details, so you can tell me where to go. Thank you for reading, back down, I want to wash in vain.

Talk about #pragma and//MARK:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.