Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up
- (void) designImageViewNow; {

//some code here

}

Is it correct to write semicolon ; just after the method name before body brackets in the implementation file objective-c.

Would this work?

As I am working on an iPhone app, I put the semicolon after the method name in one of my custom class by mistakenly. But there was no warning or any crash. In fact it is working fine.

share|improve this question
up vote 26 down vote accepted

Yes, it is acceptable syntax to do this.

In fact, I always do this in my implementations, because it then is trivial to copy and paste a method definition from interface to implementation and vice versa without having to remember where you are. It helps that I use aligned brackets on separate lines, as well.

Wil Shipley agrees with me on this:

End the definition lines on your method implementations with a semicolon, so you can copy-n-paste them to or from your header (or the "Private" category at the top of your file) as needed when they change. Semicolons are required in the "interface" section, but don't hurt anything in the "implementation" section.

share|improve this answer
    
Wil Shipley's (or anyone's) work isn't gospel though. While he's got some good conventions, other conventions are either not widely adopted, or widely adopted but disputed. For instance, in that post he recommended not to ever call self = [super init]. But it's recommended by Apple to use this in your initializer to execute code if a conditional is met. He's point however about multiple return statements in a method is widely adopted, but an ugly practise as you have to dig deep into the method just to see if it's supposed to return any other value/s. – PostCodeism Jun 27 at 17:46

Grammatically it is OK to put a semicolon there. It serves no purpose, and it is discouraged to do that. Some compilers give warnings about this extra semicolon.

share|improve this answer
1  
"It serves no purpose" - disagree. It's for convenience. When combined with aligning brackets on new lines, this allows copy/paste from interfaces to/from implementations. See Victor Ronin's answer below, which imho should be the accepted answer. – cweekly Aug 12 '14 at 14:57
1  
@cweekly its actually Brad Larson's answer – stevebot Jan 22 '15 at 20:31

Yes it will work and compile with no side effects in XCode. I see it commonly done accidentally when copying the method declaration from the header, though I wouldn't recommend adding them to the end of methods purposefully.

share|improve this answer

Yes it will work, but the semi-colon is incorrect, however it won't actually cause any crashes.

Reference link here

"Methods for a class are defined, like C functions, within a pair of braces. Before the braces, they’re declared in the same manner as in the interface file, but without the semicolon."

share|improve this answer

Code should be as clean and simple as possible.
Not only does this add litter to the code but confuses people (like me) who now have to spend the time to see if it is a valid practice.

share|improve this answer
    
Mostly agreed, but building and running is one way to see if it works, 2 sec – Stephen J Jun 25 '14 at 23:00

I'm not sure when this was introduced, but there is a new warning raised when you do this.

warn-semicolon-before-method-body

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.