Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two interfaces in *.h and *.m files

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?

like image 816
Ilya Ilin Avatar asked Jan 18 '23 07:01

Ilya Ilin


2 Answers

That's a class extension. It's usually used to declare private methods and properties for a class.

Read about it here.

like image 103
edc1591 Avatar answered Jan 25 '23 22:01

edc1591


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.

like image 27
N_A Avatar answered Jan 25 '23 22:01

N_A