Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone-为什么有些方法在.m上声明而不是在.h上声明?_Iphone - Fatal编程技术网

iPhone-为什么有些方法在.m上声明而不是在.h上声明?

iPhone-为什么有些方法在.m上声明而不是在.h上声明?,iphone,Iphone,我正在使用Xcode的基于导航的应用程序创建一个新文件,我看到.m文件有以下几行: @interface RootViewController () - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; @end 为什么在.m上声明而不是在.h上声明 在头文件上放一行(下面这一行)不是更容易吗 - (void)configureCell:(UITableViewCell *)cel

我正在使用Xcode的基于导航的应用程序创建一个新文件,我看到.m文件有以下几行:

@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
为什么在.m上声明而不是在.h上声明

在头文件上放一行(下面这一行)不是更容易吗

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
我在其他代码中见过这种方法。我仍在学习Objective-C,我想知道这是为什么


谢谢。

公共方法放在.h文件中,将私有方法放在.m文件中会使它们对其他类隐藏。

通过不将其放在类的公共接口中,实际上是将方法私有化(这不会阻止你的类之外的人调用它,如果他们真的想要的话,但至少会引起编译器警告)

是一个类扩展(=一个匿名类别;“普通”类别将在
()
)之间有一个类别名称。它的目的是声明私有方法(否则,如果在实现之前尝试调用
.m
文件中的
configureCell:atIndexPath:
),则会收到编译器警告)


您可以阅读更多关于类别和类扩展的内容

要添加的是,Objective-C中没有私有方法。因此,
.m
文件中的空类别用于声明
私有
方法。但苹果为什么要费心在模板中这样做呢?因为它真的很有用;它允许您轻松地分离/组织公共API和只有你的类才应该使用的东西。但苹果为什么要费心在模板中这样做呢?这样你就会想知道这有什么用,问问,并学习一些关于好的编码风格的东西;)
@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end