我已经下载了AA-Plot Chart的示例代码。
其中一个.h文件:
@interface MainViewController : UIViewController <APYahooDataPullerDelegate, CPPlotDataSource> {
CPLayerHostingView *layerHost;
@private
APYahooDataPuller *datapuller;
CPXYGraph *graph;
}
@property (nonatomic, retain) IBOutlet CPLayerHostingView *layerHost;
@end
在.m文件中再次声明接口?
@interface MainViewController()
@property(nonatomic, retain) CPXYGraph *graph;
@property(nonatomic, retain) APYahooDataPuller *datapuller;
@end
我在示例项目的主视图控制器代码中找到了上面的代码。
如果我执行以下操作,上述代码与以下代码之间有什么区别?
@interface MainViewController : UIViewController <APYahooDataPullerDelegate, CPPlotDataSource> {
CPLayerHostingView *layerHost;
APYahooDataPuller *datapuller;
CPXYGraph *graph;
}
@property (nonatomic, retain) IBOutlet CPLayerHostingView *layerHost;
@property(nonatomic, retain) CPXYGraph *graph;
@property(nonatomic, retain) APYahooDataPuller *datapuller;
@end
答案 0 :(得分:3)
您在.m文件中看到的“额外”内容是category。最初的程序员可能只是想从他的类的公共接口(在.h文件中)隐藏他的一些实现细节,所以他创建了一个类别(在这种情况下无名,这就是{{1}内部没有任何内容的原因。 })将它们添加到他的实现文件中。在这种特殊情况下,他隐藏了私有变量的访问器,以便外部代码无法访问它们。
您在第二个代码段中显示的更改会将所有内容放入单个类接口中。这些更改根本不应影响运行时操作。 (除了你取出()
,是故意的吗?)语义上的区别是类别方法在运行时被添加到类中。
类别只能添加方法,而不能添加实例变量,这就是原始代码在原始@private
块中具有所有实例变量声明(甚至是具有“秘密”访问器的声明)的原因。