Reputation: 2401
Sorry for my English, let speak from my heart :) In one project which I work, I noticed an interesting moment.
In *.h file declared interface:
@interface FrontViewController : UIViewController
...
@end
And in *.m file I found another interface.
@interface FrontViewController()
// Private Properties:
@property (retain, nonatomic) UIPanGestureRecognizer *navigationBarPanGestureRecognizer;
// Private Methods:
- (IBAction)pushExample:(id)sender;
@end
@implementation FrontViewController
...
@end
Why is it needed? And what's the point? -I think that this is for convenience. Yes?
Upvotes: 4
Views: 1718
Reputation: 19897
That is a class extension. It allows you to declare "private" methods and properties for a class, even if you don't have access to the source. The primary use is to not expose those methods as part of the interface. Unlike most languages, these methods are run-time discoverable, so the value of these is in the IDE auto-completion, not in preventing consumers of your class from calling the hidden methods, which is why I put private in quotes. It is possible to simply define methods in the implementation without a declaration, but then they must be implemented above any places they are used. Declaring them as an extension prevents this problem.
If an extension is named, then it becomes a category which can be used to distribute your class implementation among several files.
Upvotes: 3