开发者

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

开发者 https://www.devze.com 2023-02-15 03:56 出处:网络
I was slightly confused when, looking through some of the more obscure implementation code in one of my classes, I found several method definitions that looked like:

I was slightly confused when, looking through some of the more obscure implementation code in one of my classes, I found several method definitions that looked like:

- (void)doSomething; {
    // Something...
}

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

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

The confusion stems from the fact that these method definitions not only compile, they also execute perfectly. I saw them and immediately expected compile errors, like what you might expect if you开发者_如何学C were to attempt some C craziness like:

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

So then the question becomes, "What does that semicolon even do?" It seems like it does absolutely nothing, in which case the question becomes, "Why does the compiler allow a single random semicolon in that particular location?" No semicolons is normal, one is apparently also fine, but any more is a compile error?


It doesn't do anything. It's just to help you when you copy/paste you method declarations from your header into your implementation file. You don't have to delete the semicolon, so it saves you a couple of keystrokes. That semicolon is totally optional.

0

精彩评论

暂无评论...
验证码 换一张
取 消