Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C convention for overridden methods

In Java, when you override a method, you are advised (almost forced) to add the @Override annotation. Is there a convention to mark (either in comments, or by some other mechanism) overridden methods in the Objective-C world?

like image 668
George Penn. Avatar asked May 10 '12 14:05

George Penn.


2 Answers

No. All methods in Objective-C are sent via Objective-C's messaging, so all methods can be overridden. That's part of the language.

There's no convention for marking this, either. It's part of the language that it happens, and if you were to comment otherwise it'd just be confusing when you did it later, accidentally or intentionally.

like image 132
Steven Fisher Avatar answered Oct 21 '22 07:10

Steven Fisher


I'm not sure if Xcode does this, but the AppCode IDE from Jetbrains automatically annotates overridden methods with the little blue override badge in the margin, like so:

AppCode Override

. . further to that (also shown), I also like to create some live templates (aka code-snippets in Xcode) to annotate overridden methods with a #pragma tag. I find that it helps to define a standard structure in this order:

  • class methods
  • initialization & destruction
  • public methods / protocol methods
  • overridden methods
  • private methods

and by having Live Templates/Code Snippets I can just type 'override [tab]' and the IDE will create the #pragma tag for me.

. . perhaps you could even use OCLint to check that this structure is adhered to.

like image 1
Jasper Blues Avatar answered Oct 21 '22 07:10

Jasper Blues