What is the value of a semicolon in an Objective-C method definition?

I was a little confused when, looking through some more obscure implementation codes in one of my classes, I found several method definitions that looked like this:

- (void)doSomething; { // Something... } - (void)doSomethingWithSomething:(Something*)aSomething; { // Something with aSomething... } 

This is clearly an artifact from copying and pasting method declarations into the implementation block, and then neglecting to remove semicolons from the declaration when providing the definition.

The confusion stems from the fact that these method definitions are not only compiled, but also performed perfectly. I saw them and immediately expected compilation errors, for example, what you could expect if you tried to confuse C, for example:

 void doSomething(); { /* Something */ } 

So the question is: "What does this semicolon do?" It seems that he does absolutely nothing, and in this case the question arises: "Why does the compiler allow one random semicolon in this particular place?" There is no semicolon in norm, one, apparently, is fine too, but not a compilation error anymore?

+4
source share
1 answer

It does nothing. It just helps you when you copy / paste your method declarations from your header into your implementation file. You do not need to remove the semicolon, so it saves you a couple of keystrokes. This semicolon is completely optional.

+5
source

Source: https://habr.com/ru/post/1342694/


All Articles