@interface中的实例变量;标头与实现 [英] instance variables in @interface; header vs implementation

查看:219
本文介绍了@interface中的实例变量;标头与实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标头中声明私有实例变量与在实现中声明私有实例变量之间是否有区别?

Is there any difference between declaring a private instance variable in the header vs declaring it in the implementation?

在TestObj.h

in TestObj.h

@interface TestObj : NSObject
{
    int test;
}
@end

vs在TestObj.m

vs in TestObj.m

@interface TestObj()
{
    int test;
}
@end

这两者都与我等效,如果不优选,则在标头中声明实例变量与在实现中声明实例变量之间是否有任何实际区别?实现文件中的@interface似乎是一种声明私有属性的方法,它还有其他用途吗?

Both seem equivalent to me, is there any actual difference between declaring an instance variable in the header vs in the implementation, if not which is preferred? The @interface within the implementation file just seems like a way to declare private properties, does it have any other purpose outside that?

推荐答案

通常首选将私有实例变量和属性放在私有类扩展名(.m中)中,并保留公共接口文件(.h)用于那些真正属于公共接口的属性和方法.

The preference is generally to place private instance variables and properties in the private class extension (in the .m) and leave the public interface file (.h) for those properties and methods that are truly part of the public interface.

它有助于将实现细节与公共接口隔离开来,并使所有内容更加清晰.它还可以确保外部类不会无意间更改此类的私有变量.

It helps isolate implementation details from the public interface and makes everything much cleaner. It also ensures that external classes do not inadvertently alter the private variables of this class.

请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆