Two interfaces in *.h and *.m files

怎甘沉沦 提交于 2020-01-24 06:35:10

问题


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?


回答1:


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

Read about it here.




回答2:


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.



来源:https://stackoverflow.com/questions/9590917/two-interfaces-in-h-and-m-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!