在可可中声明私有变量的最佳方式

发布于 2024-10-31 14:49:34 字数 682 浏览 3 评论 0原文

我想知道在可可中声明私有实例变量的建议是什么。这个问题是在 iPhone 上开发应用程序的背景下提出的。

我知道至少有三种声明私有变量的方法:

  1. 在 h 文件的接口中使用修饰符 @private 声明它们:

    @interface MyClass : NSObject {  
      @私人的  
      NSObject * myPrivateVar;   
    }
    
  2. 在m文件的实现部分中声明它们:

    @implementation MyClass  
    NSObject * myPrivateVar;
    
  3. 在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:

  1. Declare them in the interface of the h file with the modifier @private:

    @interface MyClass : NSObject {  
      @private  
      NSObject * myPrivateVar;   
    }
    
  2. Declare them in the implementation section of the m file:

    @implementation MyClass  
    NSObject * myPrivateVar;
    
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城仅有旧梦在 2024-11-07 14:49:34

您的三个选项具有不同的语义:

  1. 这将创建一个实例变量。如果没有垃圾回收,您需要保留/释放存储到myPrivateVar中的对象。
  2. 根本没有定义实例变量。在 @interface 之外定义的变量和许多方法(或函数)定义的范围是“全局的”——有效的类变量(Objective-C 没有特殊的语法)。这样的变量由 MyClass 的所有实例共享。
  3. 使用属性(显式声明或不显式声明变量)之间的区别取决于内存管理。定义为 retain 意味着当您没有垃圾回收时,不需要 retain/release

所以不要使用2!如果您没有垃圾收集,选项 3 显然有好处,它提供了对选项 1 的某种抽象措施,并且成本更高 - 尽管您可能不会注意到大量访问变量的计算密集型代码之外的差异。

更新 2015

ARC(自动引用计数)之上使用垃圾收集的地方现在更适用(垃圾收集现已弃用)。现在还有第四个选项:

  1. 在 m 文件的实现部分中声明它们:

    @implementation MyClass  
    {
       NSObject * myPrivateVar;
    }
    

    与选项 (2) 不同,它声明了一个实例变量。该变量是实现私有的,并且 ARC 内存管理是自动的。这个和 (3) 之间的选择[顺便说一句,它也不再需要 @synthesize] 取决于选择和需要;属性为您提供点语法、自定义 setter 和/或 getter 的能力,以及用于在分配时自动复制的 copy 属性,但如果您不需要这些,您可以简单地使用实例变量。

Your three options have different semantics:

  1. This creates an instance variable. Without garbage collection you need to retain/release objects you store into myPrivateVar.
  2. This does not define an instance variable at all. Variables defined outside of the @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 of MyClass.
  3. The difference between using a property (with or without the variable being explicitly declared) comes down to memory management. Defined as you have with retain means that there is no need for retain/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:

  1. Declare them in the implementation section of the m file:

    @implementation MyClass  
    {
       NSObject * myPrivateVar;
    }
    

    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 the copy attribute for automatic copy on assignment, but if you need none of these you can simply use the an instance variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文