Instance variables can be declared in a class interface or implementation; however, declaring them in the public interface of a class would violate one of the key tenets of OOP—encapsulation. Therefore, the recommended practice is to declare instance variables in the class implementation; specifically, within a statement block placed immediately after the class @implementation directive.
实例变量(可理解为类成员变量)可以在接口(interface)的内部进行声明,也可以在实现部分(implementation)进行声明;
但是把实例变量声明在接口中的方式违反了面向对象的一个关键原则:封装性。
因此,建议把实例变量的声明放到类实现部分。实例变量声明紧随在@implementation 指令后面的花括号里面。如下所示
@implementation ClassName
{
// 实例变量声明的地方
}
...
@end
如果一定要在interface中声明变量,为了不影响其encapsulation 可以采用属性property的方式。