我正在使用Xcode基于导航的应用程序创建一个新文件,我看到.m文件包含以下行:
@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
为什么在.m上声明而不在.h本身上声明?
在头文件上放一行(下面这一行)不是更容易吗?
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
我在其他代码中看到过这种方法。我还在学习Objective-C,我想知道为什么会这样。
感谢。
答案 0 :(得分:4)
通过不将它放入类的公共接口,你实际上是将一个方法设为私有(它不会阻止你的类以外的人如果真的想要调用它,但至少它会导致编译器警告)。
@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
是类扩展(=匿名类别;“普通”类别在()
之间具有类别名称)。它的目的是声明私有方法(否则,如果您在实现之前尝试在configureCell:atIndexPath:
文件中调用.m
,则会收到编译器警告。)
您可以详细了解类别和类扩展in the developer documentation
答案 1 :(得分:3)
公共方法放在.h文件中,将.m文件中的私有方法隐藏在其他类中。