在可可中声明私有变量的最佳方式
我想知道在可可中声明私有实例变量的建议是什么。这个问题是在 iPhone 上开发应用程序的背景下提出的。
我知道至少有三种声明私有变量的方法:
在 h 文件的接口中使用修饰符 @private 声明它们:
@interface MyClass : NSObject { @私人的 NSObject * myPrivateVar; }
在m文件的实现部分中声明它们:
@implementation MyClass NSObject * myPrivateVar;
在m文件的接口中声明一个属性(甚至不声明变量本身):
@interfaceMyClass() @property(非原子,保留)NSString * myPrivateVar; @结尾 @执行 @synthesize myPrivateVar;
到目前为止,我已经广泛使用了2,但最近意识到这可能是危险的,因为缺少垃圾收藏。是否存在使用该方法仍然完全可以接受的情况?
3个比较合适吗?答案是否取决于对象类型(例如可变/不可变)?
一般而言,讨论使用/不使用属性的权衡的参考材料的指针也值得赞赏。
I would like to know what the recommendations are for declaring private instance variables in cocoa. This question is in the context of developing apps on the iPhone.
I am aware of at least three ways of declaring private variables:
Declare them in the interface of the h file with the modifier @private:
@interface MyClass : NSObject { @private NSObject * myPrivateVar; }
Declare them in the implementation section of the m file:
@implementation MyClass NSObject * myPrivateVar;
Declare a property in the interface of the m file (not even declaring the variable itself):
@interface MyClass () @property (nonatomic, retain) NSString* myPrivateVar; @end @implementation @synthesize myPrivateVar;
So far, I have used extensively 2 but recently came to realize this might be dangerous due to the lack of garbage collection. Are there cases where it remains perfectly acceptable to use that method?
Is 3 more appropriate? Does the answer depend on the object type (e.g. mutable/immutable)?
Pointers to reference material discussing the trade offs for using/not using properties in general also appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的三个选项具有不同的语义:
保留
/释放
存储到myPrivateVar
中的对象。MyClass
的所有实例共享。retain
意味着当您没有垃圾回收时,不需要retain
/release
。所以不要使用2!如果您没有垃圾收集,选项 3 显然有好处,它提供了对选项 1 的某种抽象措施,并且成本更高 - 尽管您可能不会注意到大量访问变量的计算密集型代码之外的差异。
更新 2015
在 ARC(自动引用计数)之上使用垃圾收集的地方现在更适用(垃圾收集现已弃用)。现在还有第四个选项:
在 m 文件的实现部分中声明它们:
与选项 (2) 不同,它声明了一个实例变量。该变量是实现私有的,并且 ARC 内存管理是自动的。这个和 (3) 之间的选择[顺便说一句,它也不再需要
@synthesize
] 取决于选择和需要;属性为您提供点语法、自定义 setter 和/或 getter 的能力,以及用于在分配时自动复制的copy
属性,但如果您不需要这些,您可以简单地使用实例变量。Your three options have different semantics:
retain
/release
objects you store intomyPrivateVar
.@interface
and the scope of many method (or function) definitions are "global" - effectively class variables (which Objective-C has no special syntax for). Such a variable is shared by all instances ofMyClass
.retain
means that there is no need forretain
/release
when you do not have garbage collection.So don't use 2! Option 3 clearly has benefits if you don't have garbage collection, it provides some measure of abstraction over option 1, and is more costly - though you will probably not notice the difference outside of computationally intensive code which access the variable heavily.
Update 2015
Where garbage collection is used above ARC (automatic reference counting) is now more applicable (garbage collection is now deprecated). Also there is now a fourth option:
Declare them in the implementation section of the m file:
Unlike option (2) this does declare an instance variable. The variable is private to the implementation, and with ARC memory management is automatic. The choice between this and (3) [which incidentally also no longer requires the
@synthesize
] comes down to choice and need; properties give you dot syntax, the ability to customise the setter and/or getter, and thecopy
attribute for automatic copy on assignment, but if you need none of these you can simply use the an instance variable.