自从开始研究iOS应用程序和目标C以来,我一直对可以声明和定义变量的不同位置感到困惑.一方面我们采用传统的C方法,另一方面我们有新的ObjectiveC指令,在其上添加OO.你能不能帮助我了解最佳实践和情况,我希望将这些位置用于我的变量,或者纠正我目前的理解?
这是一个示例类(.h和.m):
#import <Foundation/Foundation.h>
// 1) What do I declare here?
@interface SampleClass : NSObject
{
// 2) ivar declarations
// Pretty much never used?
}
// 3) class-specific method / property declarations
@end
Run Code Online (Sandbox Code Playgroud)
和
#import "SampleClass.h"
// 4) what goes here?
@interface SampleClass()
// 5) private interface, can define private methods and properties here
@end
@implementation SampleClass
{
// 6) define ivars
}
// 7) define methods and synthesize properties from both public and private
// interfaces
@end
Run Code Online (Sandbox Code Playgroud)