Search code examples
objective-cclass-extensions

What is the purpose of a class extension without any content?


According to developer documentation, a class extension is implemented by declaring an @interface in the implementation file, and it can also be used to redeclare instance variables to be private. However, I frequently see the code below that does not declare new methods or instance variables. What is its purpose?

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController 
...  

Solution

  • There is no purpose behind such code. The only reason it is there is that it is part of the standard template for creating .m files in Xcode.

    With this said, such class extensions are entirely harmless, so keeping them in case you need to add private methods or variables does not hurt performance of your app. In the end, it is a matter of personal taste: for example, I remove such unused template-generated artifacts from my code, but I can accept an argument in favor of keeping them as well.

    If you do not wish to have these class extensions generated by default, clone and modify Xcode template for new Objective-C classes (here is a Q&A explaining how to do it).